|
@@ -2,6 +2,8 @@ import numpy as np
|
|
|
|
|
|
class Gate:
|
|
|
def __init__(self, gate):
|
|
|
+ gate = np.array(gate)
|
|
|
+
|
|
|
if gate.shape != (2, 2):
|
|
|
raise ValueError(
|
|
|
"Gate is not a 2x2 matrix. " +
|
|
@@ -10,7 +12,7 @@ class Gate:
|
|
|
)
|
|
|
|
|
|
# Check the gate is unitary
|
|
|
- if (not np.allclose(np.eye(gate.shape[0]), gate.H * gate)):
|
|
|
+ if (not np.allclose(np.eye(gate.shape[0]), np.dot(gate.conjugate().transpose(), gate))):
|
|
|
raise ValueError("gate is not unitary.")
|
|
|
|
|
|
self.a = complex(gate[0, 0])
|
|
@@ -22,22 +24,22 @@ class Gate:
|
|
|
return '[{:.4f}, {:.4f}]\n[{:.4f}, {:.4f}]'.format(self.a, self.b, self.c, self.d)
|
|
|
|
|
|
def h():
|
|
|
- return Gate(np.matrix([[1, 1], [1, -1]]) / np.sqrt(2))
|
|
|
+ return Gate(np.array([[1, 1], [1, -1]]) / np.sqrt(2))
|
|
|
|
|
|
def x():
|
|
|
- return Gate(np.matrix([[0, 1], [1, 0]]))
|
|
|
+ return Gate(np.array([[0, 1], [1, 0]]))
|
|
|
|
|
|
def y():
|
|
|
- return Gate(np.matrix([[0, -1j], [1j, 0]]))
|
|
|
+ return Gate(np.array([[0, -1j], [1j, 0]]))
|
|
|
|
|
|
def z():
|
|
|
- return Gate(np.matrix([[1, 0], [0, -1]]))
|
|
|
+ return Gate(np.array([[1, 0], [0, -1]]))
|
|
|
|
|
|
def s():
|
|
|
- return Gate(np.matrix([[1, 0], [0, 1j]]))
|
|
|
+ return Gate(np.array([[1, 0], [0, 1j]]))
|
|
|
|
|
|
def t():
|
|
|
- return Gate(np.matrix([[1, 0], [0, np.exp(np.pi * 1j / 4)]]))
|
|
|
+ return Gate(np.array([[1, 0], [0, np.exp(np.pi * 1j / 4)]]))
|
|
|
|
|
|
def sqrt_x():
|
|
|
- return Gate(0.5 * np.matrix([[1+1j, 1-1j], [1-1j, 1+1j]]))
|
|
|
+ return Gate(0.5 * np.array([[1+1j, 1-1j], [1-1j, 1+1j]]))
|