def test_sympy_backend_for_one_qubit_gate(): E = eye(2) X = gate.X(0).get_target_matrix() Y = gate.Y(0).get_target_matrix() Z = gate.Z(0).get_target_matrix() H = gate.H(0).get_target_matrix() T = gate.T(0).get_target_matrix() S = gate.S(0).get_target_matrix() x, y, z = symbols('x, y, z') RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]]) RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]]) RZ = Matrix([[exp(-I * z / 2), 0], [0, exp(I * z / 2)]]) actual_1 = Circuit().x[0, 1].y[1].z[2].run(backend="sympy_unitary") expected_1 = reduce(TensorProduct, [Z, Y * X, X]) assert actual_1 == expected_1 actual_2 = Circuit().y[0].z[3].run(backend="sympy_unitary") expected_2 = reduce(TensorProduct, [Z, E, E, Y]) assert actual_2 == expected_2 actual_3 = Circuit().x[0].z[3].h[:].t[1].s[2].run(backend="sympy_unitary") expected_3 = reduce(TensorProduct, [H * Z, S * H, T * H, H * X]) assert actual_3 == expected_3 actual_4 = Circuit().rx(-pi / 2)[0].rz( pi / 2)[1].ry(pi)[2].run(backend="sympy_unitary") expected_4 = reduce(TensorProduct, [RY, RZ, RX]).subs([[x, -pi / 2], [y, pi], [z, pi / 2]]) assert actual_4 == expected_4
def __init__(self): try: lazy_import() except ImportError: raise ImportError('sympy_unitary requires sympy. Please install before call this option.') theta, phi, lambd = symbols('theta phi lambd') self.theta = theta self.phi = phi self.lambd = lambd self.SYMPY_GATE = { 'X': sympy_gate.X(0).get_target_matrix(), 'Y': sympy_gate.Y(0).get_target_matrix(), 'Z': sympy_gate.Z(0).get_target_matrix(), 'H': sympy_gate.H(0).get_target_matrix(), 'T': sympy_gate.T(0).get_target_matrix(), 'S': sympy_gate.S(0).get_target_matrix(), 'RX': Matrix([[cos(theta / 2), -I * sin(theta / 2)], [-I * sin(theta / 2), cos(theta / 2)]]), 'RY': Matrix([[cos(theta / 2), -sin(theta / 2)], [sin(theta / 2), cos(theta / 2)]]), 'RZ': Matrix([[exp(-I * theta / 2), 0], [0, exp(I * theta / 2)]]), 'TARGET_CX': sympy_gate.X(0).get_target_matrix(), 'TARGET_CZ': sympy_gate.Z(0).get_target_matrix(), 'U1': Matrix([[exp(-I * lambd / 2), 0], [0, exp(I * lambd / 2)]]), 'U2': Matrix([ [exp(-I * (phi + lambd) / 2) / sqrt(2), -exp(-I * (phi - lambd) / 2) / sqrt(2)], [exp(I * (phi - lambd) / 2) / sqrt(2), exp(I * (phi + lambd) / 2) / sqrt(2)]]), 'U3': Matrix([ [exp(-I * (phi + lambd) / 2) * cos(theta / 2), -exp(-I * (phi - lambd) / 2) * sin(theta / 2)], [exp(I * (phi - lambd) / 2) * sin(theta / 2), exp(I * (phi + lambd) / 2) * cos(theta / 2)]]), }
def eqsup(n): state = Qubit('0'*n) for i in range(n): state = gate.H(i) * state return qapply(state)
def test_sympy_backend_for_two_qubit_gate(): E = eye(2) UPPER = Matrix([[1, 0], [0, 0]]) LOWER = Matrix([[0, 0], [0, 1]]) X = gate.X(0).get_target_matrix() Z = gate.Z(0).get_target_matrix() H = gate.H(0).get_target_matrix() H_3 = reduce(TensorProduct, [H, E, H]) H_4 = reduce(TensorProduct, [H, E, E, H]) CX_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce( TensorProduct, [X, E, LOWER]) CZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce( TensorProduct, [Z, E, LOWER]) CX_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce( TensorProduct, [X, E, E, LOWER]) CZ_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce( TensorProduct, [Z, E, E, LOWER]) actual_1 = Circuit().cx[0, 3].run(backend="sympy_unitary") assert actual_1 == CX_4 actual_2 = Circuit().cx[1, 3].x[4].run(backend="sympy_unitary") expected_2 = reduce(TensorProduct, [X, CX_3, E]) assert actual_2 == expected_2 actual_3 = Circuit().cz[0, 3].run(backend="sympy_unitary") assert actual_3 == CZ_4 actual_4 = Circuit().cz[1, 3].x[4].run(backend="sympy_unitary") expected_4 = reduce(TensorProduct, [X, CZ_3, E]) assert actual_4 == expected_4 actual_5 = Circuit().cx[3, 0].run(backend="sympy_unitary") assert actual_5 == H_4 * CX_4 * H_4 actual_6 = Circuit().cx[3, 1].x[4].run(backend="sympy_unitary") assert actual_6 == reduce(TensorProduct, [X, H_3 * CX_3 * H_3, E]) actual_7 = Circuit().cz[3, 0].run(backend="sympy_unitary") assert actual_7 == CZ_4 actual_8 = Circuit().cz[3, 1].x[4].run(backend="sympy_unitary") assert actual_8 == reduce(TensorProduct, [X, CZ_3, E])
def test_sympy_backend_for_two_qubit_gate(): E = eye(2) UPPER = Matrix([[1, 0], [0, 0]]) LOWER = Matrix([[0, 0], [0, 1]]) X = gate.X(0).get_target_matrix() Z = gate.Z(0).get_target_matrix() H = gate.H(0).get_target_matrix() H_3 = reduce(TensorProduct, [H, E, H]) H_4 = reduce(TensorProduct, [H, E, E, H]) CX_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce( TensorProduct, [X, E, LOWER]) CZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce( TensorProduct, [Z, E, LOWER]) CX_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce( TensorProduct, [X, E, E, LOWER]) CZ_4 = reduce(TensorProduct, [E, E, E, UPPER]) + reduce( TensorProduct, [Z, E, E, LOWER]) actual_1 = Circuit().cx[0, 3].run(backend="sympy_unitary") assert actual_1 == CX_4 actual_2 = Circuit().cx[1, 3].x[4].run(backend="sympy_unitary") expected_2 = reduce(TensorProduct, [X, CX_3, E]) assert actual_2 == expected_2 actual_3 = Circuit().cz[0, 3].run(backend="sympy_unitary") assert actual_3 == CZ_4 actual_4 = Circuit().cz[1, 3].x[4].run(backend="sympy_unitary") expected_4 = reduce(TensorProduct, [X, CZ_3, E]) assert actual_4 == expected_4 actual_5 = Circuit().cx[3, 0].run(backend="sympy_unitary") assert actual_5 == H_4 * CX_4 * H_4 actual_6 = Circuit().cx[3, 1].x[4].run(backend="sympy_unitary") assert actual_6 == reduce(TensorProduct, [X, H_3 * CX_3 * H_3, E]) actual_7 = Circuit().cz[3, 0].run(backend="sympy_unitary") assert actual_7 == CZ_4 actual_8 = Circuit().cz[3, 1].x[4].run(backend="sympy_unitary") assert actual_8 == reduce(TensorProduct, [X, CZ_3, E]) x, y, z = symbols('x, y, z') RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]]) RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]]) RZ = Matrix([[exp(-I * z / 2), 0], [0, exp(I * z / 2)]]) CRX_3 = reduce(TensorProduct, [UPPER, E, E]) + reduce( TensorProduct, [LOWER, E, RX]) CRY_4 = reduce(TensorProduct, [E, UPPER, E, E]) + reduce( TensorProduct, [E, LOWER, RY, E]) CRZ_3 = reduce(TensorProduct, [E, E, UPPER]) + reduce( TensorProduct, [RZ, E, LOWER]) actual_9 = Circuit().crx(x)[2, 0].run(backend="sympy_unitary") assert simplify(actual_9) == CRX_3 actual_10 = Circuit().cry(y)[2, 1].i[3].run(backend="sympy_unitary") assert simplify(actual_10) == CRY_4 actual_11 = Circuit().crz(z)[0, 2].run(backend="sympy_unitary") assert simplify(actual_11) == CRZ_3