Skip to content

Commit 15f0cc8

Browse files
Merge pull request #32 from tqsd/custom_gates_on_qubits
Adds custom gate logic to the qubits
2 parents 72c54f8 + 7eb298c commit 15f0cc8

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

objects/qubit.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import numpy as np
23

34

45
class Qubit(object):
@@ -205,6 +206,59 @@ def cphase(self, target):
205206
"""
206207
self._host.backend.cphase(self, target)
207208

209+
def custom_gate(self, gate):
210+
"""
211+
Applies a custom 2x2 unitary on the qubit.
212+
213+
Args:
214+
gate (Numpy ndarray): A unitary 2x2 matrix
215+
"""
216+
217+
if not isinstance(gate, np.ndarray):
218+
raise (InputError("Only Numpy matrices are allowed"))
219+
if not is_unitary(gate):
220+
raise (InputError("Custom gates must be unitary operations"))
221+
if gate.shape != (2, 2):
222+
raise (InputError("Custom gates must be 2x2 matrices"))
223+
224+
self._host.backend.custom_gate(self, gate)
225+
226+
def custom_controlled_gate(self, target, gate):
227+
"""
228+
Applies a custom 2x2 unitary on the qubit.
229+
230+
Args:
231+
target (Qubit): Qubit on which the controlled gate should be applied.
232+
gate (Numpy ndarray): A unitary 2x2 matrix
233+
"""
234+
235+
if not isinstance(gate, np.ndarray):
236+
raise (InputError("Only Numpy arrays are allowed"))
237+
if not is_unitary(gate):
238+
raise (InputError("Custom gates must be unitary operations"))
239+
if gate.shape != (2, 2):
240+
raise (InputError("Custom controlled gates must be 2x2 matrices"))
241+
242+
self._host.backend.custom_controlled_gate(self, target, gate)
243+
244+
def custom_two_qubit_gate(self, other_qubit, gate):
245+
"""
246+
Applies a custom 2 qubit gate.
247+
248+
Args:
249+
other_qubit (Qubit): The second qubit.
250+
gate (Numpy ndarray): The gate
251+
252+
"""
253+
if not isinstance(gate, np.ndarray):
254+
raise (InputError("Only Numpy matrices are allowed"))
255+
if not is_unitary(gate):
256+
raise (InputError("Custom gates must be unitary operations"))
257+
if gate.shape != (4, 4):
258+
raise (InputError("Custom controlled gates must be 2x2 matrices"))
259+
260+
self._host.backend.custom_two_qubit_gate(self, other_qubit, gate)
261+
208262
def measure(self, non_destructive=False):
209263
"""
210264
Measures the state of a qubit.
@@ -217,3 +271,20 @@ def measure(self, non_destructive=False):
217271
measured_value (int): 0 or 1, dependent on measurement outcome.
218272
"""
219273
return self._host.backend.measure(self, non_destructive)
274+
275+
276+
def is_unitary(m):
277+
return np.allclose(np.eye(m.shape[0]), m.conj().T.dot(m))
278+
279+
280+
class InputError(Exception):
281+
"""
282+
Exception raised for errors in the input.
283+
284+
Attributes:
285+
expression -- input expression in which the error occurred
286+
message -- explanation of the error
287+
"""
288+
289+
def __init__(self, message):
290+
self.message = message

0 commit comments

Comments
 (0)