-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathx_basis.py
More file actions
275 lines (198 loc) · 7.46 KB
/
x_basis.py
File metadata and controls
275 lines (198 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright 2023 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, Iterable, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union
import numpy as np
from attrs import frozen
from numpy.typing import NDArray
from qualtran import (
AddControlledT,
Bloq,
bloq_example,
BloqBuilder,
BloqDocSpec,
ConnectionT,
CtrlSpec,
QBit,
Register,
Side,
Signature,
SoquetT,
)
from qualtran.drawing import directional_text_box, Text, WireSymbol
if TYPE_CHECKING:
import cirq
import pyzx as zx
import quimb.tensor as qtn
from qualtran.cirq_interop import CirqQuregT
from qualtran.simulation.classical_sim import ClassicalValT
_PLUS = np.ones(2, dtype=np.complex128) / np.sqrt(2)
_MINUS = np.array([1, -1], dtype=np.complex128) / np.sqrt(2)
_PAULIX = np.array([[0, 1], [1, 0]], dtype=np.complex128)
@frozen
class _XVector(Bloq):
"""The |+> or |-> state or effect.
Please use the explicitly named subclasses instead of the boolean arguments.
Args:
bit: False chooses |+>, True chooses |->
state: True means this is a state with right registers; False means this is an
effect with left registers.
n: bitsize of the vector.
"""
bit: bool
state: bool = True
n: int = 1
def __attrs_post_init__(self):
if self.n != 1:
raise NotImplementedError("Come back later.")
@cached_property
def signature(self) -> 'Signature':
return Signature([Register('q', QBit(), side=Side.RIGHT if self.state else Side.LEFT)])
def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
) -> List['qtn.Tensor']:
import quimb.tensor as qtn
side = outgoing if self.state else incoming
return [
qtn.Tensor(data=_MINUS if self.bit else _PLUS, inds=[(side['q'], 0)], tags=[str(self)])
]
def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT' # type: ignore[type-var]
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]: # type: ignore[type-var]
if not self.state:
raise ValueError(f"There is no Cirq equivalent for {self}")
import cirq
(q,) = qubit_manager.qalloc(self.n)
if self.bit:
op = cirq.CircuitOperation(cirq.FrozenCircuit(cirq.X(q), cirq.H(q)))
else:
op = cirq.H(q)
return op, {'q': np.array([q])}
def __str__(self) -> str:
s = '-' if self.bit else '+'
return f'|{s}>' if self.state else f'<{s}|'
def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
) -> 'WireSymbol':
if reg is None:
return Text('')
s = '-' if self.bit else '+'
return directional_text_box(s, side=reg.side)
def _hide_base_fields(cls, fields):
# for use in attrs `field_trasnformer`.
return [
field.evolve(repr=False) if field.name in ['bit', 'state'] else field for field in fields
]
@frozen(init=False, field_transformer=_hide_base_fields)
class PlusState(_XVector):
"""The state |+>"""
def __init__(self, n: int = 1):
self.__attrs_init__(bit=False, state=True, n=n)
def adjoint(self) -> 'Bloq':
return PlusEffect()
@bloq_example
def _plus_state() -> PlusState:
plus_state = PlusState()
return plus_state
_PLUS_STATE_DOC = BloqDocSpec(bloq_cls=PlusState, examples=[_plus_state])
@frozen(init=False, field_transformer=_hide_base_fields)
class PlusEffect(_XVector):
"""The effect <+|"""
def __init__(self, n: int = 1):
self.__attrs_init__(bit=False, state=False, n=n)
def adjoint(self) -> 'Bloq':
return PlusState()
@bloq_example
def _plus_effect() -> PlusEffect:
plus_effect = PlusEffect()
return plus_effect
_PLUS_EFFECT_DOC = BloqDocSpec(bloq_cls=PlusEffect, examples=[_plus_effect])
@frozen(init=False, field_transformer=_hide_base_fields)
class MinusState(_XVector):
"""The state |->"""
def __init__(self, n: int = 1):
self.__attrs_init__(bit=True, state=True, n=n)
def adjoint(self) -> 'Bloq':
return MinusEffect()
@bloq_example
def _minus_state() -> MinusState:
minus_state = MinusState()
return minus_state
_MINUS_STATE_DOC = BloqDocSpec(bloq_cls=MinusState, examples=[_minus_state])
@frozen(init=False, field_transformer=_hide_base_fields)
class MinusEffect(_XVector):
"""The effect <-|"""
def __init__(self, n: int = 1):
self.__attrs_init__(bit=True, state=False, n=n)
def adjoint(self) -> 'Bloq':
return MinusState()
@bloq_example
def _minus_effect() -> MinusEffect:
minus_effect = MinusEffect()
return minus_effect
_MINUS_EFFECT_DOC = BloqDocSpec(bloq_cls=MinusEffect, examples=[_minus_effect])
@frozen
class XGate(Bloq):
"""The Pauli X gate.
This causes a bit flip: X|0> = |1> and vice-versa.
"""
@cached_property
def signature(self) -> 'Signature':
return Signature.build(q=1)
def adjoint(self) -> 'Bloq':
return self
def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
) -> List['qtn.Tensor']:
import quimb.tensor as qtn
return [
qtn.Tensor(
data=_PAULIX, inds=[(outgoing['q'], 0), (incoming['q'], 0)], tags=[str(self)]
)
]
def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> Tuple['Bloq', 'AddControlledT']:
from qualtran.bloqs.basic_gates import CNOT, Toffoli
if ctrl_spec == CtrlSpec():
bloq: 'Bloq' = CNOT()
elif ctrl_spec == CtrlSpec(cvs=(1, 1)):
bloq = Toffoli()
else:
return super().get_ctrl_system(ctrl_spec)
def add_controlled(
bb: 'BloqBuilder', ctrl_soqs: Sequence['SoquetT'], in_soqs: Dict[str, 'SoquetT']
) -> Tuple[Iterable['SoquetT'], Iterable['SoquetT']]:
(ctrl_soq,) = ctrl_soqs
ctrl_soq, target = bb.add(bloq, ctrl=ctrl_soq, target=in_soqs['q'])
return (ctrl_soq,), (target,)
return bloq, add_controlled
def on_classical_vals(self, q: int) -> Dict[str, 'ClassicalValT']:
return {'q': (q + 1) % 2}
def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT'
) -> Tuple['cirq.Operation', Dict[str, 'CirqQuregT']]:
import cirq
q = cirq_quregs.pop('q')
(q,) = q
return cirq.X(q), {'q': np.asarray([q])}
def wire_symbol(self, reg: Register, idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
from qualtran.drawing import ModPlus
if reg is None:
return Text('X')
return ModPlus()
def as_zx_gates(
self, ancilla_manager, /, q: NDArray[np.integer]
) -> tuple[list['zx.circuit.Gate'], dict[str, NDArray[np.integer]]]:
import pyzx as zx
(qubit,) = q
return [zx.circuit.NOT(qubit)], {'q': q}