def test_linalg(self):
     A = QuantumGate(array([[0.0, 1.0], [0.0, 0.0]]))
     B = QuantumGate(array([[0.0, 0.0], [1.0, 0.0]]))
     self.assertTrue((A.H().matrix() == B.matrix()).all())
     X = A + B
     self.assertTrue((X.matrix() == x_gate().matrix()).all())
     self.assertTrue((dot(X, X).matrix() == eye(2)).all())
    def test_x_gate(self):
        X = x_gate()
        self.assertTrue((X.matrix() == array([[0., 1.], [1., 0.]])).all())

        # Test that applying the X-gate to a qubit in state |0> converts it to
        # state |1>, and vice versa
        q = QubitSystem()
        X * q
        self.assertEqual(q.measure(), 1)
        X * q
        self.assertEqual(q.measure(), 0)
    def test_act(self):
        q1 = QubitSystem(2)
        q2 = QubitSystem(2, 0b10)
        SWAP = swap_gate()
        CNOT = cnot_gate()

        # SWAP * |00> = |00>
        SWAP * q1
        self.assertEqual(q1.measure(), 0b00)

        # SWAP * |10> = |01>, and vice versa
        SWAP * q2
        self.assertEqual(q2.measure(), 0b01)
        SWAP * q2
        self.assertEqual(q2.measure(), 0b10)

        # CNOT * |00> = |00>
        CNOT * q1
        self.assertEqual(q1.measure(), 0b00)

        # CNOT * |10> = |11> and vice versa
        CNOT * q2
        self.assertEqual(q2.measure(), 0b11)
        CNOT * q2
        self.assertEqual(q2.measure(), 0b10)

        # Test applying a gate to a subset of the qubits in the system
        q = QubitSystem(4)  # state 0000
        X = x_gate()

        X.act(q, 3)
        self.assertEqual(q.measure(), 0b0010)
        X.act(q, 4)
        self.assertEqual(q.measure(), 0b0011)
        SWAP.act(q, 2)
        self.assertEqual(q.measure(), 0b0101)
        SWAP * q
        self.assertEqual(q.measure(), 0b1001)