-
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
Changes from 75 commits
a9d5d7f
62e9fee
ac2db81
fbca42b
4dfe53a
0f3b5a1
d8bbe49
a747b51
497d7ee
9bba4e4
efe6f0f
e145433
dedc304
9fc5300
e0f2b41
c58116c
535c8ef
9e97cc9
f283249
a26cdab
f9bd77d
a10a331
1c758d4
f57cdcc
d0773f1
ba39913
300b8ba
bad1f36
d668e62
e699973
85907da
96f2cbb
919bdd9
b05d94d
40ef237
22e3184
aabe689
70bac9a
6e93114
8951104
9e1bb8c
7492027
8fb94c6
3a395f7
fa68016
63ed9b4
9aad5f0
7f40996
5e499dc
32485e3
34a5325
4a8b431
d27a5c1
1b22928
d75ef07
2d310aa
04b5ab0
678ba7b
bb6b510
6de6b8e
5c79a89
d0b77d6
6b7fb0f
3d09d66
9e62a54
615d42c
de0c07a
51d9fb6
580c599
e1ff3f3
6dce631
841c6c9
4a13cb5
7a10e93
aaf7eff
b673149
a20db85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright 2024 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))] | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||
| # Copyright 2024 Google LLC | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| # | ||||||
| # 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) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,18 @@ | |
| # limitations under the License. | ||
|
|
||
| from functools import cached_property | ||
| from typing import cast, Dict, Iterable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union | ||
| from typing import ( | ||
| cast, | ||
| Dict, | ||
| Iterable, | ||
| List, | ||
| Mapping, | ||
| Optional, | ||
| Sequence, | ||
| Tuple, | ||
| TYPE_CHECKING, | ||
| Union, | ||
| ) | ||
|
|
||
| import attrs | ||
| import numpy as np | ||
|
|
@@ -27,6 +38,7 @@ | |
| bloq_example, | ||
| BloqBuilder, | ||
| BloqDocSpec, | ||
| CBit, | ||
| CompositeBloq, | ||
| ConnectionT, | ||
| CtrlSpec, | ||
|
|
@@ -52,7 +64,7 @@ | |
|
|
||
| from qualtran.cirq_interop import CirqQuregT | ||
| from qualtran.resource_counting import BloqCountDictT, SympySymbolAllocator | ||
| from qualtran.simulation.classical_sim import ClassicalValT | ||
| from qualtran.simulation.classical_sim import ClassicalValRetT, ClassicalValT | ||
|
|
||
| _ZERO = np.array([1, 0], dtype=np.complex128) | ||
| _ONE = np.array([0, 1], dtype=np.complex128) | ||
|
|
@@ -379,6 +391,44 @@ def _cz() -> CZ: | |
| _CZ_DOC = BloqDocSpec(bloq_cls=CZ, examples=[_cz], call_graph_example=None) | ||
|
|
||
|
|
||
| @frozen | ||
| class MeasZ(Bloq): | ||
| """Measure a qubit in the Z basis. | ||
| Registers: | ||
| q [LEFT]: The qubit to measure. | ||
| c [RIGHT]: The classical measurement result. | ||
| """ | ||
|
|
||
| @cached_property | ||
| def signature(self) -> 'Signature': | ||
| return Signature( | ||
| [Register('q', QBit(), side=Side.LEFT), Register('c', CBit(), side=Side.RIGHT)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ) | ||
|
|
||
| def on_classical_vals(self, q: int) -> Mapping[str, 'ClassicalValRetT']: | ||
| return {'c': q} | ||
|
|
||
| def my_tensors( | ||
| self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT'] | ||
| ) -> List['qtn.Tensor']: | ||
| import quimb.tensor as qtn | ||
|
|
||
| from qualtran.simulation.tensor import DiscardInd | ||
|
|
||
| copy = np.zeros((2, 2, 2), dtype=np.complex128) | ||
| copy[0, 0, 0] = 1 | ||
| copy[1, 1, 1] = 1 | ||
| # Tie together q, c, and meas_result with the copy tensor; throw out one of the legs. | ||
| meas_result = qtn.rand_uuid('meas_result') | ||
| t = qtn.Tensor( | ||
| data=copy, | ||
| inds=[(incoming['q'], 0), (outgoing['c'], 0), (meas_result, 0)], | ||
| tags=[str(self)], | ||
| ) | ||
| return [t, DiscardInd((meas_result, 0))] | ||
|
|
||
|
|
||
| @frozen | ||
| class _IntVector(Bloq): | ||
| """Represent a classical non-negative integer vector (state or effect). | ||
|
|
||
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.