-
Notifications
You must be signed in to change notification settings - Fork 269
/
_qubit.py
executable file
·228 lines (187 loc) · 6.58 KB
/
_qubit.py
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
# 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
#
# http://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.
"""
This file defines BasicQubit, Qubit, WeakQubit and Qureg.
A Qureg represents a list of Qubit or WeakQubit objects.
Qubit represents a (logical-level) qubit with a unique index provided by the
MainEngine. Qubit objects are automatically deallocated if they go out of
scope and intented to be used within Qureg objects in user code.
Example:
.. code-block:: python
from projectq import MainEngine
eng = MainEngine()
qubit = eng.allocate_qubit()
qubit is a Qureg of size 1 with one Qubit object which is deallocated once
qubit goes out of scope.
WeakQubit are used inside the Command object and are not automatically
deallocated.
"""
class BasicQubit(object):
"""
BasicQubit objects represent qubits.
They have an id and a reference to the owning engine.
"""
def __init__(self, engine, idx):
"""
Initialize a BasicQubit object.
Args:
engine: Owning engine / engine that created the qubit
idx: Unique index of the qubit referenced by this qubit
"""
self.id = idx
self.engine = engine
def __str__(self):
"""
Return string representation of this qubit.
"""
return str(self.id)
def __bool__(self):
"""
Access the result of a previous measurement and return False / True
(0 / 1)
"""
return self.engine.main_engine.get_measurement_result(self)
def __nonzero__(self):
"""
Access the result of a previous measurement for Python 2.7.
"""
return self.__bool__()
def __int__(self):
"""
Access the result of a previous measurement and return as integer
(0 / 1).
"""
return int(bool(self))
def __eq__(self, other):
"""
Compare with other qubit (Returns True if equal id and engine).
Args:
other (BasicQubit): BasicQubit to which to compare this one
"""
if self.id == -1:
return self is other
return (isinstance(other, BasicQubit) and
self.id == other.id and
self.engine == other.engine)
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
"""
Return the hash of this qubit.
Hash definition because of custom __eq__.
Enables storing a qubit in, e.g., a set.
"""
if self.id == -1:
return object.__hash__(self)
return hash((self.engine, self.id))
class Qubit(BasicQubit):
"""
Qubit class.
Represents a (logical-level) qubit with a unique index provided by the
MainEngine. Once the qubit goes out of scope (and is garbage-collected),
it deallocates itself automatically, allowing automatic resource
management.
Thus the qubit is not copyable; only returns a reference to the same
object.
"""
def __del__(self):
"""
Destroy the qubit and deallocate it (automatically).
"""
self.engine.deallocate_qubit(self)
self.id = -1
def __copy__(self):
"""
Non-copyable (returns reference to self).
Note:
To prevent problems with automatic deallocation, qubits are not
copyable!
"""
return self
def __deepcopy__(self, memo):
"""
Non-deepcopyable (returns reference to self).
Note:
To prevent problems with automatic deallocation, qubits are not
deepcopyable!
"""
return self
class WeakQubitRef(BasicQubit):
"""
WeakQubitRef objects are used inside the Command object.
Qubits feature automatic deallocation when destroyed. WeakQubitRefs, on
the other hand, do not share this feature, allowing to copy them and pass
them along the compiler pipeline, while the actual qubit objects may be
garbage-collected (and, thus, cleaned up early). Otherwise there is no
difference between a WeakQubitRef and a Qubit object.
"""
pass
class Qureg(list):
"""
Quantum register class.
Simplifies accessing measured values for single-qubit registers (no []-
access necessary) and enables pretty-printing of general quantum registers
(call Qureg.__str__(qureg)).
"""
def __bool__(self):
"""
Return measured value if Qureg consists of 1 qubit only.
Raises:
Exception if more than 1 qubit resides in this register (then you
need to specify which value to get using qureg[???])
"""
if len(self) == 1:
return bool(self[0])
else:
raise Exception("__bool__(qureg): Quantum register contains more "
"than 1 qubit. Use __bool__(qureg[idx]) instead.")
def __int__(self):
"""
Return measured value if Qureg consists of 1 qubit only.
Raises:
Exception if more than 1 qubit resides in this register (then you
need to specify which value to get using qureg[???])
"""
if len(self) == 1:
return int(self[0])
else:
raise Exception("__int__(qureg): Quantum register contains more "
"than 1 qubit. Use __bool__(qureg[idx]) instead.")
def __nonzero__(self):
"""
Return measured value if Qureg consists of 1 qubit only for Python 2.7.
Raises:
Exception if more than 1 qubit resides in this register (then you
need to specify which value to get using qureg[???])
"""
return int(self) != 0
def __str__(self):
"""
Get string representation of a quantum register.
"""
if len(self) == 1:
return "Qubit[" + str(self[0]) + "]"
else:
return "Qureg" + str([q.id for q in self])
@property
def engine(self):
"""
Return owning engine.
"""
return self[0].engine
@engine.setter
def engine(self, eng):
"""
Set owning engine.
"""
for qb in self:
qb.engine = eng