def test_cgate(): """Test the general CGate.""" # Test single control functionality CNOTMatrix = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) assert represent(CGate(1, XGate(0)), nqubits=2) == CNOTMatrix # Test multiple control bit functionality ToffoliGate = CGate((1, 2), XGate(0)) assert represent(ToffoliGate, nqubits=3) == Matrix([ [1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], ]) ToffoliGate = CGate((3, 0), XGate(1)) assert qapply(ToffoliGate * Qubit("1001")) == matrix_to_qubit( represent(ToffoliGate * Qubit("1001"), nqubits=4)) assert qapply(ToffoliGate * Qubit("0000")) == matrix_to_qubit( represent(ToffoliGate * Qubit("0000"), nqubits=4)) CYGate = CGate(1, YGate(0)) CYGate_matrix = Matrix( ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 0, -I), (0, 0, I, 0))) # Test 2 qubit controlled-Y gate decompose method. assert represent(CYGate.decompose(), nqubits=2) == CYGate_matrix CZGate = CGate(0, ZGate(1)) CZGate_matrix = Matrix( ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, -1))) assert qapply(CZGate * Qubit("11")) == -Qubit("11") assert matrix_to_qubit(represent(CZGate * Qubit("11"), nqubits=2)) == -Qubit("11") # Test 2 qubit controlled-Z gate decompose method. assert represent(CZGate.decompose(), nqubits=2) == CZGate_matrix CPhaseGate = CGate(0, PhaseGate(1)) assert qapply(CPhaseGate * Qubit("11")) == I * Qubit("11") assert matrix_to_qubit(represent(CPhaseGate * Qubit("11"), nqubits=2)) == I * Qubit("11") # Test that the dagger, inverse, and power of CGate is evaluated properly assert Dagger(CZGate) == CZGate assert pow(CZGate, 1) == Dagger(CZGate) assert Dagger(CZGate) == CZGate.inverse() assert Dagger(CPhaseGate) != CPhaseGate assert Dagger(CPhaseGate) == CPhaseGate.inverse() assert Dagger(CPhaseGate) == pow(CPhaseGate, -1) assert pow(CPhaseGate, -1) == CPhaseGate.inverse()
def test_apply_represent_equality(): gates = [ HadamardGate(int(3 * random.random())), XGate(int(3 * random.random())), ZGate(int(3 * random.random())), YGate(int(3 * random.random())), ZGate(int(3 * random.random())), PhaseGate(int(3 * random.random())), ] circuit = Qubit( int(random.random() * 2), int(random.random() * 2), int(random.random() * 2), int(random.random() * 2), int(random.random() * 2), int(random.random() * 2), ) for i in range(int(random.random() * 6)): circuit = gates[int(random.random() * 6)] * circuit mat = represent(circuit, nqubits=6) states = qapply(circuit) state_rep = matrix_to_qubit(mat) states = states.expand() state_rep = state_rep.expand() assert state_rep == states
def test_cgate(): """Test the general CGate.""" # Test single control functionality CNOTMatrix = Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) assert represent(CGate(1, XGate(0)), nqubits=2) == CNOTMatrix # Test multiple control bit functionality ToffoliGate = CGate((1, 2), XGate(0)) assert represent(ToffoliGate, nqubits=3) == \ Matrix([[1,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0],[0,0,1,0,0,0,0,0],\ [0,0,0,1,0,0,0,0],[0,0,0,0,1,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1],\ [0,0,0,0,0,0,1,0]]) ToffoliGate = CGate((3, 0), XGate(1)) assert qapply(ToffoliGate*Qubit('1001')) == \ matrix_to_qubit(represent(ToffoliGate*Qubit('1001'), nqubits=4)) assert qapply(ToffoliGate*Qubit('0000')) == \ matrix_to_qubit(represent(ToffoliGate*Qubit('0000'), nqubits=4)) CYGate = CGate(1, YGate(0)) CYGate_matrix = Matrix( ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 0, -I), (0, 0, I, 0))) # Test 2 qubit controlled-Y gate decompose method. assert represent(CYGate.decompose(), nqubits=2) == CYGate_matrix CZGate = CGate(0, ZGate(1)) CZGate_matrix = Matrix( ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, -1))) assert qapply(CZGate * Qubit('11')) == -Qubit('11') assert matrix_to_qubit(represent(CZGate*Qubit('11'),nqubits=2)) ==\ -Qubit('11') # Test 2 qubit controlled-Z gate decompose method. assert represent(CZGate.decompose(), nqubits=2) == CZGate_matrix CPhaseGate = CGate(0, PhaseGate(1)) assert qapply(CPhaseGate*Qubit('11')) ==\ I*Qubit('11') assert matrix_to_qubit(represent(CPhaseGate*Qubit('11'), nqubits=2)) == \ I*Qubit('11')
def test_unitary_XGate(): x = XGate(1, 2) x_dagger = Dagger(x) assert (x * x_dagger == 1)
def test_hermitian_XGate(): x = XGate(1, 2) x_dagger = Dagger(x) assert (x == x_dagger)
def test_compound_gates(): """Test a compound gate representation.""" circuit = YGate(0) * ZGate(0) * XGate(0) * HadamardGate(0) * Qubit('00') answer = represent(circuit, nqubits=2) assert Matrix([I / sqrt(2), I / sqrt(2), 0, 0]) == answer
def test_represent_xgate(): """Test the representation of the X gate.""" circuit = XGate(0) * Qubit('00') answer = represent(circuit, nqubits=2) assert Matrix([0, 1, 0, 0]) == answer
def test_sympy__physics__quantum__gate__XGate(): from sympy.physics.quantum.gate import XGate assert _test_args(XGate(0))
def test_gate(): a, b, c, d = symbols('a,b,c,d') uMat = Matrix([[a, b], [c, d]]) q = Qubit(1, 0, 1, 0, 1) g1 = IdentityGate(2) g2 = CGate((3, 0), XGate(1)) g3 = CNotGate(1, 0) g4 = UGate((0, ), uMat) assert str(g1) == '1(2)' assert pretty(g1) == '1 \n 2' assert upretty(g1) == u'1 \n 2' assert latex(g1) == r'1_{2}' sT(g1, "IdentityGate(Integer(2))") assert str(g1 * q) == '1(2)*|10101>' ascii_str = \ """\ 1 *|10101>\n\ 2 \ """ ucode_str = \ u"""\ 1 ⋅❘10101⟩\n\ 2 \ """ assert pretty(g1 * q) == ascii_str assert upretty(g1 * q) == ucode_str assert latex(g1 * q) == r'1_{2} {\left|10101\right\rangle }' sT( g1 * q, "Mul(IdentityGate(Integer(2)), Qubit(Integer(1),Integer(0),Integer(1),Integer(0),Integer(1)))" ) assert str(g2) == 'C((3,0),X(1))' ascii_str = \ """\ C /X \\\n\ 3,0\\ 1/\ """ ucode_str = \ u"""\ C ⎛X ⎞\n\ 3,0⎝ 1⎠\ """ assert pretty(g2) == ascii_str assert upretty(g2) == ucode_str assert latex(g2) == r'C_{3,0}{\left(X_{1}\right)}' sT(g2, "CGate(Tuple(Integer(3), Integer(0)),XGate(Integer(1)))") assert str(g3) == 'CNOT(1,0)' ascii_str = \ """\ CNOT \n\ 1,0\ """ ucode_str = \ u"""\ CNOT \n\ 1,0\ """ assert pretty(g3) == ascii_str assert upretty(g3) == ucode_str assert latex(g3) == r'CNOT_{1,0}' sT(g3, "CNotGate(Integer(1),Integer(0))") # str(g4) Fails #assert str(g4) == '' ascii_str = \ """\ U \n\ 0\ """ ucode_str = \ u"""\ U \n\ 0\ """ assert pretty(g4) == ascii_str assert upretty(g4) == ucode_str assert latex(g4) == r'U_{0}' sT( g4, "UGate(Tuple(Integer(0)),MutableDenseMatrix([[Symbol('a'), Symbol('b')], [Symbol('c'), Symbol('d')]]))" )
def test_gate(): a, b, c, d = symbols("a,b,c,d") uMat = Matrix([[a, b], [c, d]]) q = Qubit(1, 0, 1, 0, 1) g1 = IdentityGate(2) g2 = CGate((3, 0), XGate(1)) g3 = CNotGate(1, 0) g4 = UGate((0, ), uMat) assert str(g1) == "1(2)" assert pretty(g1) == "1 \n 2" assert upretty(g1) == u"1 \n 2" assert latex(g1) == r"1_{2}" sT(g1, "IdentityGate(Integer(2))") assert str(g1 * q) == "1(2)*|10101>" ascii_str = """\ 1 *|10101>\n\ 2 \ """ ucode_str = u("""\ 1 ⋅❘10101⟩\n\ 2 \ """) assert pretty(g1 * q) == ascii_str assert upretty(g1 * q) == ucode_str assert latex(g1 * q) == r"1_{2} {\left|10101\right\rangle }" sT( g1 * q, "Mul(IdentityGate(Integer(2)), Qubit(Integer(1),Integer(0),Integer(1),Integer(0),Integer(1)))", ) assert str(g2) == "C((3,0),X(1))" ascii_str = """\ C /X \\\n\ 3,0\\ 1/\ """ ucode_str = u("""\ C ⎛X ⎞\n\ 3,0⎝ 1⎠\ """) assert pretty(g2) == ascii_str assert upretty(g2) == ucode_str assert latex(g2) == r"C_{3,0}{\left(X_{1}\right)}" sT(g2, "CGate(Tuple(Integer(3), Integer(0)),XGate(Integer(1)))") assert str(g3) == "CNOT(1,0)" ascii_str = """\ CNOT \n\ 1,0\ """ ucode_str = u("""\ CNOT \n\ 1,0\ """) assert pretty(g3) == ascii_str assert upretty(g3) == ucode_str assert latex(g3) == r"CNOT_{1,0}" sT(g3, "CNotGate(Integer(1),Integer(0))") ascii_str = """\ U \n\ 0\ """ ucode_str = u("""\ U \n\ 0\ """) assert (str(g4) == """\ U((0,),Matrix([\n\ [a, b],\n\ [c, d]]))\ """) assert pretty(g4) == ascii_str assert upretty(g4) == ucode_str assert latex(g4) == r"U_{0}" sT( g4, "UGate(Tuple(Integer(0)),MutableDenseMatrix([[Symbol('a'), Symbol('b')], [Symbol('c'), Symbol('d')]]))", )