def test_apply_operator(self): """Tests apply_operator() since it's executed on other processors.""" vec = numpy.array([1, 2, 3, 4]) matvec_expected = numpy.array([2, 1, 4, 3]) self.assertTrue(numpy.allclose( apply_operator((LinearQubitOperator(QubitOperator('X1')), vec)), matvec_expected))
def test_matvec_zx(self): """Testing with multiple factors.""" vec = numpy.array([1, 2, 3, 4]) matvec_expected = numpy.array([2, 1, -4, -3]) self.assertTrue(numpy.allclose( LinearQubitOperator(QubitOperator('Z0 X1')) * vec, matvec_expected))
def test_matvec_z(self): """Testing product with Z.""" vec = numpy.array([1, 2, 3, 4]) matvec_expected = numpy.array([1, 2, -3, -4]) self.assertTrue(numpy.allclose( LinearQubitOperator(QubitOperator('Z0'), 2) * vec, matvec_expected))
def test_matvec_y(self): """Testing product with Y.""" vec = numpy.array([1, 2, 3, 4], dtype=complex) matvec_expected = 1.0j * numpy.array([-2, 1, -4, 3], dtype=complex) self.assertTrue(numpy.allclose( LinearQubitOperator(QubitOperator('Y1')) * vec, matvec_expected))
def test_matvec_x(self): """Testing product with X.""" vec = numpy.array([1, 2, 3, 4]) matvec_expected = numpy.array([2, 1, 4, 3]) self.assertTrue(numpy.allclose( LinearQubitOperator(QubitOperator('X1')) * vec, matvec_expected))
def test_matvec_0(self): """Testing with zero term.""" qubit_operator = QubitOperator.zero() vec = numpy.array([1, 2, 3, 4, 5, 6, 7, 8]) matvec_expected = numpy.zeros(vec.shape) self.assertTrue(numpy.allclose( LinearQubitOperator(qubit_operator, 3) * vec, matvec_expected))
def test_matvec_compare(self): """Compare LinearQubitOperator with qubit_operator_sparse.""" qubit_operator = QubitOperator('X0 Y1 Z3') mat_expected = qubit_operator_sparse(qubit_operator) self.assertTrue(numpy.allclose(numpy.transpose( numpy.array([LinearQubitOperator(qubit_operator) * v for v in numpy.identity(16)])), mat_expected.A))
def test_matvec_z3(self): """Testing product with Z^n.""" vec = numpy.array( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) matvec_expected = numpy.array( [1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16]) self.assertTrue(numpy.allclose( LinearQubitOperator(QubitOperator('Z3')) * vec, matvec_expected))
def test_matvec_multiple_terms(self): """Testing with multiple terms.""" qubit_operator = (QubitOperator.identity() + 2 * QubitOperator('Y2') + QubitOperator(((0, 'Z'), (1, 'X')), 10.0)) vec = numpy.array([1, 2, 3, 4, 5, 6, 7, 8]) matvec_expected = (10 * numpy.array([3, 4, 1, 2, -7, -8, -5, -6]) + 2j * numpy.array([-2, 1, -4, 3, -6, 5, -8, 7]) + vec) self.assertTrue(numpy.allclose( LinearQubitOperator(qubit_operator) * vec, matvec_expected))
def test_init(self): """Tests __init__().""" qubit_operator = QubitOperator('Z2') n_qubits = 3 linear_operator = LinearQubitOperator(qubit_operator) self.assertEqual(linear_operator.qubit_operator, qubit_operator) self.assertEqual(linear_operator.n_qubits, n_qubits) # Checks type. self.assertTrue( isinstance(linear_operator, scipy.sparse.linalg.LinearOperator))
def test_matvec_wrong_vec_length(self): """Testing with wrong vector length.""" with self.assertRaises(ValueError): _ = LinearQubitOperator(QubitOperator('X3')) * numpy.zeros(4)
def test_matvec_wrong_n(self): """Testing with wrong n_qubits.""" with self.assertRaises(ValueError): LinearQubitOperator(QubitOperator('X3'), 1)