def test_circuit_op_to_matrix(self):
     """test CircuitOp.to_matrix"""
     qc = QuantumCircuit(1)
     qc.rz(1.0, 0)
     qcop = CircuitOp(qc)
     np.testing.assert_array_almost_equal(
         qcop.to_matrix(), scipy.linalg.expm(-0.5j * Z.to_matrix()))
    def test_evals(self):
        """ evals test """
        # TODO: Think about eval names
        self.assertEqual(Z.eval('0').eval('0'), 1)
        self.assertEqual(Z.eval('1').eval('0'), 0)
        self.assertEqual(Z.eval('0').eval('1'), 0)
        self.assertEqual(Z.eval('1').eval('1'), -1)
        self.assertEqual(X.eval('0').eval('0'), 0)
        self.assertEqual(X.eval('1').eval('0'), 1)
        self.assertEqual(X.eval('0').eval('1'), 1)
        self.assertEqual(X.eval('1').eval('1'), 0)
        self.assertEqual(Y.eval('0').eval('0'), 0)
        self.assertEqual(Y.eval('1').eval('0'), -1j)
        self.assertEqual(Y.eval('0').eval('1'), 1j)
        self.assertEqual(Y.eval('1').eval('1'), 0)

        with self.assertRaises(ValueError):
            Y.eval('11')

        with self.assertRaises(ValueError):
            (X ^ Y).eval('1111')

        with self.assertRaises(ValueError):
            Y.eval((X ^ X).to_matrix_op())

        # Check that Pauli logic eval returns same as matrix logic
        self.assertEqual(PrimitiveOp(Z.to_matrix()).eval('0').eval('0'), 1)
        self.assertEqual(PrimitiveOp(Z.to_matrix()).eval('1').eval('0'), 0)
        self.assertEqual(PrimitiveOp(Z.to_matrix()).eval('0').eval('1'), 0)
        self.assertEqual(PrimitiveOp(Z.to_matrix()).eval('1').eval('1'), -1)
        self.assertEqual(PrimitiveOp(X.to_matrix()).eval('0').eval('0'), 0)
        self.assertEqual(PrimitiveOp(X.to_matrix()).eval('1').eval('0'), 1)
        self.assertEqual(PrimitiveOp(X.to_matrix()).eval('0').eval('1'), 1)
        self.assertEqual(PrimitiveOp(X.to_matrix()).eval('1').eval('1'), 0)
        self.assertEqual(PrimitiveOp(Y.to_matrix()).eval('0').eval('0'), 0)
        self.assertEqual(PrimitiveOp(Y.to_matrix()).eval('1').eval('0'), -1j)
        self.assertEqual(PrimitiveOp(Y.to_matrix()).eval('0').eval('1'), 1j)
        self.assertEqual(PrimitiveOp(Y.to_matrix()).eval('1').eval('1'), 0)

        pauli_op = Z ^ I ^ X ^ Y
        mat_op = PrimitiveOp(pauli_op.to_matrix())
        full_basis = list(map(''.join, itertools.product('01', repeat=pauli_op.num_qubits)))
        for bstr1, bstr2 in itertools.product(full_basis, full_basis):
            # print('{} {} {} {}'.format(bstr1, bstr2, pauli_op.eval(bstr1, bstr2),
            # mat_op.eval(bstr1, bstr2)))
            np.testing.assert_array_almost_equal(pauli_op.eval(bstr1).eval(bstr2),
                                                 mat_op.eval(bstr1).eval(bstr2))

        gnarly_op = SummedOp([(H ^ I ^ Y).compose(X ^ X ^ Z).tensor(Z),
                              PrimitiveOp(Operator.from_label('+r0I')),
                              3 * (X ^ CX ^ T)], coeff=3 + .2j)
        gnarly_mat_op = PrimitiveOp(gnarly_op.to_matrix())
        full_basis = list(map(''.join, itertools.product('01', repeat=gnarly_op.num_qubits)))
        for bstr1, bstr2 in itertools.product(full_basis, full_basis):
            np.testing.assert_array_almost_equal(gnarly_op.eval(bstr1).eval(bstr2),
                                                 gnarly_mat_op.eval(bstr1).eval(bstr2))
Пример #3
0
    def test_qpe_Z1(self, backend_type):
        """eigenproblem Z, |1>"""
        backend = qiskit.BasicAer.get_backend(backend_type)

        unitary_circuit = Z.to_circuit()
        state_preparation = X.to_circuit()  # prepare |1>
        phase = self.one_phase(unitary_circuit,
                               state_preparation,
                               backend=backend)
        self.assertEqual(phase, 0.5)
Пример #4
0
 def test_qpe_Z(self, state_preparation, expected_phase, backend_type,
                phase_estimator):
     """eigenproblem Z, |0> and |1>"""
     unitary_circuit = Z.to_circuit()
     phase = self.one_phase(
         unitary_circuit,
         state_preparation,
         backend_type=backend_type,
         phase_estimator=phase_estimator,
     )
     self.assertEqual(phase, expected_phase)
    def test_to_matrix(self):
        """to matrix text"""
        np.testing.assert_array_equal(X.to_matrix(),
                                      Operator.from_label("X").data)
        np.testing.assert_array_equal(Y.to_matrix(),
                                      Operator.from_label("Y").data)
        np.testing.assert_array_equal(Z.to_matrix(),
                                      Operator.from_label("Z").data)

        op1 = Y + H
        np.testing.assert_array_almost_equal(op1.to_matrix(),
                                             Y.to_matrix() + H.to_matrix())

        op2 = op1 * 0.5
        np.testing.assert_array_almost_equal(op2.to_matrix(),
                                             op1.to_matrix() * 0.5)

        op3 = (4 - 0.6j) * op2
        np.testing.assert_array_almost_equal(op3.to_matrix(),
                                             op2.to_matrix() * (4 - 0.6j))

        op4 = op3.tensor(X)
        np.testing.assert_array_almost_equal(
            op4.to_matrix(), np.kron(op3.to_matrix(), X.to_matrix()))

        op5 = op4.compose(H ^ I)
        np.testing.assert_array_almost_equal(
            op5.to_matrix(), np.dot(op4.to_matrix(), (H ^ I).to_matrix()))

        op6 = op5 + PrimitiveOp(Operator.from_label("+r").data)
        np.testing.assert_array_almost_equal(
            op6.to_matrix(),
            op5.to_matrix() + Operator.from_label("+r").data)

        param = Parameter("α")
        m = np.array([[0, -1j], [1j, 0]])
        op7 = MatrixOp(m, param)
        np.testing.assert_array_equal(op7.to_matrix(), m * param)

        param = Parameter("β")
        op8 = PauliOp(primitive=Pauli("Y"), coeff=param)
        np.testing.assert_array_equal(op8.to_matrix(), m * param)

        param = Parameter("γ")
        qc = QuantumCircuit(1)
        qc.h(0)
        op9 = CircuitOp(qc, coeff=param)
        m = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
        np.testing.assert_array_equal(op9.to_matrix(), m * param)
Пример #6
0
    def test_qpe_Zplus(self):
        """superposition eigenproblem Z, |+>"""
        unitary_circuit = Z.to_circuit()
        state_preparation = H.to_circuit()  # prepare |+>
        result = self.phase_estimation(
            unitary_circuit,
            state_preparation,
            backend=qiskit.BasicAer.get_backend('statevector_simulator'))

        phases = result.filter_phases(1e-15, as_float=True)
        with self.subTest('test phases has correct values'):
            self.assertEqual(list(phases.keys()), [0.0, 0.5])

        with self.subTest('test phases has correct probabilities'):
            np.testing.assert_allclose(list(phases.values()), [0.5, 0.5])

        with self.subTest('test bitstring representation'):
            phases = result.filter_phases(1e-15, as_float=False)
            self.assertEqual(list(phases.keys()), ['000000', '100000'])
Пример #7
0
    def test_is_hermitian(self):
        """Test is_hermitian method."""
        with self.subTest("I"):
            self.assertTrue(I.is_hermitian())

        with self.subTest("X"):
            self.assertTrue(X.is_hermitian())

        with self.subTest("Y"):
            self.assertTrue(Y.is_hermitian())

        with self.subTest("Z"):
            self.assertTrue(Z.is_hermitian())

        with self.subTest("XY"):
            self.assertFalse((X @ Y).is_hermitian())

        with self.subTest("CX"):
            self.assertTrue(CX.is_hermitian())

        with self.subTest("T"):
            self.assertFalse(T.is_hermitian())
Пример #8
0
    def test_pauli_I_Z(self):
        """test from_ising and to_ising with Pauli I and Z"""
        with self.subTest("0 * I, linear=False"):
            q_p = from_ising(0 * I, linear=False)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 0)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((2, 2)))
            self.assertAlmostEqual(offset, 0)

        with self.subTest("0 * I, linear=True"):
            q_p = from_ising(0 * I, linear=True)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 0)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((2, 2)))
            self.assertAlmostEqual(offset, 0)

        with self.subTest("2 * I, linear=False"):
            q_p = from_ising(2 * I, linear=False)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 2)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((2, 2)))
            self.assertAlmostEqual(offset, 2)

        with self.subTest("2 * I, linear=True"):
            q_p = from_ising(2 * I, linear=True)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 2)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((2, 2)))
            self.assertAlmostEqual(offset, 2)

        with self.subTest("Z, linear=False"):
            q_p = from_ising(Z)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 1)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[-2]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), Z.to_matrix())
            self.assertAlmostEqual(offset, 0)

        with self.subTest("Z, linear=True"):
            q_p = from_ising(Z, linear=True)
            self.assertEqual(q_p.get_num_vars(), 1)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 1)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [-2])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), Z.to_matrix())
            self.assertAlmostEqual(offset, 0)

        with self.subTest("3 * II, linear=False"):
            q_p = from_ising(3 * I ^ I)
            self.assertEqual(q_p.get_num_vars(), 2)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 3)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0, 0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0, 0], [0, 0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((4, 4)))
            self.assertAlmostEqual(offset, 3)

        with self.subTest("3 * II, linear=True"):
            q_p = from_ising(3 * I ^ I)
            self.assertEqual(q_p.get_num_vars(), 2)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 3)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0, 0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0, 0], [0, 0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), np.zeros((4, 4)))
            self.assertAlmostEqual(offset, 3)

        with self.subTest("IZ, linear=False"):
            q_p = from_ising(I ^ Z)
            self.assertEqual(q_p.get_num_vars(), 2)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 1)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [0, 0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[-2, 0], [0, 0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), (I ^ Z).to_matrix())
            self.assertAlmostEqual(offset, 0)

        with self.subTest("IZ, linear=True"):
            q_p = from_ising(I ^ Z, linear=True)
            self.assertEqual(q_p.get_num_vars(), 2)
            self.assertEqual(q_p.get_num_linear_constraints(), 0)
            self.assertEqual(q_p.get_num_quadratic_constraints(), 0)
            self.assertAlmostEqual(q_p.objective.constant, 1)
            np.testing.assert_allclose(q_p.objective.linear.to_array(), [-2, 0])
            np.testing.assert_allclose(q_p.objective.quadratic.to_array(), [[0, 0], [0, 0]])

            op, offset = to_ising(q_p)
            np.testing.assert_allclose(op.to_matrix(), (I ^ Z).to_matrix())
            self.assertAlmostEqual(offset, 0)
Пример #9
0
 def test_exp_i(self):
     """ exponential of Pauli test """
     op = Z.exp_i()
     gate = op.to_circuit().data[0][0]
     self.assertIsInstance(gate, qiskit.circuit.library.RZGate)
     self.assertEqual(gate.params[0], 2)