-
Notifications
You must be signed in to change notification settings - Fork 103
[open-systems] Superoperator tensor contraction #1596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
a9d5d7f
notey
mpharrigan 62e9fee
ctrl
mpharrigan ac2db81
qdtype
mpharrigan fbca42b
ctrl spec
mpharrigan 4dfe53a
controlled
mpharrigan 0f3b5a1
controlled
mpharrigan d8bbe49
data types
mpharrigan a747b51
on classical vals
mpharrigan 497d7ee
dtypes
mpharrigan 9bba4e4
registers
mpharrigan efe6f0f
registers
mpharrigan e145433
bloq ctrl spec
mpharrigan dedc304
cast
mpharrigan 9fc5300
bloq dtypes
mpharrigan e0f2b41
controlled
mpharrigan c58116c
bloq dtypes
mpharrigan 535c8ef
ctrl
mpharrigan 9e97cc9
bloq dtypes
mpharrigan f283249
bloq dtypes
mpharrigan a26cdab
bloq dtypes
mpharrigan f9bd77d
bloq dtypes
mpharrigan a10a331
bloq dtypes
mpharrigan 1c758d4
bloq dtypes
mpharrigan f57cdcc
drawing
mpharrigan d0773f1
drawing
mpharrigan ba39913
annotations
mpharrigan 300b8ba
bits
mpharrigan bad1f36
bits
mpharrigan d668e62
init
mpharrigan e699973
init
mpharrigan 85907da
controlled docs
mpharrigan 96f2cbb
lint
mpharrigan 919bdd9
fixes
mpharrigan b05d94d
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan 40ef237
format
mpharrigan 22e3184
autogenerate notebooks
mpharrigan aabe689
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan 70bac9a
add notebook to toc
mpharrigan 6e93114
fix musical score serialization
mpharrigan 8951104
phased classical sim
mpharrigan 9e1bb8c
docstring
mpharrigan 7492027
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan 8fb94c6
Merge branch '2025-03/classical-1' into 2025-03/classical-sim-1
mpharrigan 3a395f7
stuff
mpharrigan fa68016
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan 63ed9b4
Merge branch '2025-03/classical-1' into 2025-03/classical-sim-1
mpharrigan 9aad5f0
superquimb
mpharrigan 7f40996
measz
mpharrigan 5e499dc
discard
mpharrigan 32485e3
gates init
mpharrigan 34a5325
bloq
mpharrigan 4a8b431
flatten including cbloqs
mpharrigan d27a5c1
new quimb api
mpharrigan 1b22928
polish
mpharrigan d75ef07
lint
mpharrigan 2d310aa
use a typealias for the "Any" quimb ind
mpharrigan 04b5ab0
fixes
mpharrigan 678ba7b
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan bb6b510
Merge branch '2025-03/classical-1' into 2025-03/classical-sim-1
mpharrigan 6de6b8e
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan 5c79a89
Merge remote-tracking branch 'origin/main' into 2025-03/classical-1
mpharrigan d0b77d6
Merge branch '2025-03/classical-1' into 2025-03/classical-sim-1
mpharrigan 6b7fb0f
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan 3d09d66
Signature.n_xbits
mpharrigan 9e62a54
Merge branch '2025-03/classical-1' into 2025-03/classical-sim-1
mpharrigan 615d42c
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan de0c07a
Merge remote-tracking branch 'origin/main' into 2025-03/classical-sim-1
mpharrigan 51d9fb6
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan 580c599
Merge remote-tracking branch 'origin/main' into 2025-03/classical-sim-1
mpharrigan e1ff3f3
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan 6dce631
Merge remote-tracking branch 'origin/main' into 2025-03/classical-sim-1
mpharrigan 841c6c9
Merge branch '2025-03/classical-sim-1' into 2025-03/open-quimb-1
mpharrigan 4a13cb5
Merge remote-tracking branch 'origin/main' into 2025-03/open-quimb-1
mpharrigan 7a10e93
Merge remote-tracking branch 'origin/main' into 2025-03/open-quimb-1
mpharrigan aaf7eff
Merge remote-tracking branch 'origin/main' into 2025-03/open-quimb-1
mpharrigan b673149
fix copyrights
mpharrigan a20db85
Merge remote-tracking branch 'origin/main' into 2025-03/open-quimb-1
mpharrigan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from functools import cached_property | ||
| from typing import Dict, List, TYPE_CHECKING | ||
|
|
||
| from attrs import frozen | ||
|
|
||
| from qualtran import Bloq, CBit, ConnectionT, QBit, Register, Side, Signature | ||
| from qualtran.simulation.classical_sim import ClassicalValT | ||
|
|
||
| if TYPE_CHECKING: | ||
| from qualtran.simulation.tensor import DiscardInd | ||
|
|
||
|
|
||
| @frozen | ||
| class Discard(Bloq): | ||
| """Discard a classical bit. | ||
|
|
||
| This is an allowed operation. | ||
| """ | ||
|
|
||
| @cached_property | ||
| def signature(self) -> 'Signature': | ||
| return Signature([Register('c', CBit(), side=Side.LEFT)]) | ||
|
|
||
| def on_classical_vals(self, c: int) -> Dict[str, 'ClassicalValT']: | ||
| return {} | ||
|
|
||
| def my_tensors( | ||
| self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT'] | ||
| ) -> List['DiscardInd']: | ||
|
|
||
| from qualtran.simulation.tensor import DiscardInd | ||
|
|
||
| return [DiscardInd((incoming['c'], 0))] | ||
|
|
||
|
|
||
| @frozen | ||
| class DiscardQ(Bloq): | ||
| """Discard a qubit. | ||
|
|
||
| This is a dangerous operation that can ruin your computation. This is equivalent to | ||
| measuring the qubit and throwing out the measurement operation, so it removes any coherences | ||
| involved with the qubit. Use with care. | ||
| """ | ||
|
|
||
| @cached_property | ||
| def signature(self) -> 'Signature': | ||
| return Signature([Register('q', QBit(), side=Side.LEFT)]) | ||
|
|
||
| def my_tensors( | ||
| self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT'] | ||
| ) -> List['DiscardInd']: | ||
|
|
||
| from qualtran.simulation.tensor import DiscardInd | ||
|
|
||
| return [DiscardInd((incoming['q'], 0))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from qualtran import BloqBuilder | ||
| from qualtran.bloqs.basic_gates import ( | ||
| CNOT, | ||
| Discard, | ||
| DiscardQ, | ||
| MeasZ, | ||
| PlusState, | ||
| ZeroEffect, | ||
| ZeroState, | ||
| ) | ||
|
|
||
|
|
||
| def test_discard(): | ||
| bb = BloqBuilder() | ||
| q = bb.add(ZeroState()) | ||
| c = bb.add(MeasZ(), q=q) | ||
| bb.add(Discard(), c=c) | ||
| cbloq = bb.finalize() | ||
|
|
||
| # We're allowed to discard classical bits in the classical simulator | ||
| ret = cbloq.call_classically() | ||
| assert ret == () | ||
|
|
||
| k = cbloq.tensor_contract(superoperator=True) | ||
| np.testing.assert_allclose(k, 1.0, atol=1e-8) | ||
|
|
||
|
|
||
| def test_discard_vs_project(): | ||
| # Using the ZeroState effect un-physically projects us, giving trace of 0.5 | ||
| bb = BloqBuilder() | ||
| q = bb.add(PlusState()) | ||
| bb.add(ZeroEffect(), q=q) | ||
| cbloq = bb.finalize() | ||
| k = cbloq.tensor_contract(superoperator=True) | ||
| np.testing.assert_allclose(k, 0.5, atol=1e-8) | ||
|
|
||
| # Measure and discard is trace preserving | ||
| bb = BloqBuilder() | ||
| q = bb.add(PlusState()) | ||
| c = bb.add(MeasZ(), q=q) | ||
| bb.add(Discard(), c=c) | ||
| cbloq = bb.finalize() | ||
| k = cbloq.tensor_contract(superoperator=True) | ||
| np.testing.assert_allclose(k, 1.0, atol=1e-8) | ||
|
|
||
|
|
||
| def test_discardq(): | ||
| # Completely dephasing map | ||
| # https://learning.quantum.ibm.com/course/general-formulation-of-quantum-information/quantum-channels#the-completely-dephasing-channel | ||
| bb = BloqBuilder() | ||
| q = bb.add_register('q', 1) | ||
| env = bb.add(ZeroState()) | ||
| q, env = bb.add(CNOT(), ctrl=q, target=env) | ||
| bb.add(DiscardQ(), q=env) | ||
| cbloq = bb.finalize(q=q) | ||
| ss = cbloq.tensor_contract(superoperator=True) | ||
|
|
||
| should_be = np.zeros((2, 2, 2, 2)) | ||
| should_be[0, 0, 0, 0] = 1 | ||
| should_be[1, 1, 1, 1] = 1 | ||
|
|
||
| np.testing.assert_allclose(ss, should_be, atol=1e-8) | ||
|
|
||
| # Classical simulator will not let you throw out qubits | ||
| with pytest.raises(NotImplementedError, match=r'.*classical simulation.*'): | ||
| _ = cbloq.call_classically(q=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if the measurement is nondemolition measurement then the qubit can be reused. which would be simpler than allocating a new qubit in the state indicated by the classical bit.
perhabs a boolean that controls whether the qubit is LEFT or TRHOUGH can be used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is certainly a valid operation, although I'd advocate for that being its own bloq.
The current construct is simpler if you're measuring a qubit to get its bit value