def test_chop_all(self):
     """Test that chop returns an identity operator with coeff 0 if all coeffs are chopped."""
     eps = 1e-10
     op = SparsePauliOp(["X", "Z"], coeffs=[eps, eps])
     simplified = op.chop(tol=eps)
     expected = SparsePauliOp(["I"], coeffs=[0.0])
     self.assertEqual(simplified, expected)
 def to_list(self):
     """Test to_operator method."""
     labels = ["XI", "YZ", "YY", "ZZ"]
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     op = SparsePauliOp(labels, coeffs)
     target = list(zip(labels, coeffs))
     self.assertEqual(op.to_list(), target)
 def test_label_iter(self):
     """Test SparsePauliOp label_iter method."""
     labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     op = SparsePauliOp(labels, coeffs)
     for idx, i in enumerate(op.label_iter()):
         self.assertEqual(i, (labels[idx], coeffs[idx]))
示例#4
0
 def to_list(self):
     """Test to_operator method."""
     labels = ['XI', 'YZ', 'YY', 'ZZ']
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     op = SparsePauliOp(PauliTable.from_labels(labels), coeffs)
     target = list(zip(labels, coeffs))
     self.assertEqual(op.to_list(), target)
示例#5
0
 def test_matrix_iter_sparse(self):
     """Test SparsePauliOp sparse matrix_iter method."""
     labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     op = SparsePauliOp(labels, coeffs)
     for idx, i in enumerate(op.matrix_iter(sparse=True)):
         np.testing.assert_array_equal(i.toarray(), coeffs[idx] * pauli_mat(labels[idx]))
 def test_iter(self):
     """Test iter with SparsePauliOp."""
     labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     op = SparsePauliOp(labels, coeffs)
     for idx, i in enumerate(iter(op)):
         self.assertEqual(i, SparsePauliOp(labels[idx], coeffs[[idx]]))
 def test_from_index_list_same_index(self):
     """Test from_list via Pauli + number of qubits raises correctly, if indices duplicate."""
     with self.assertRaises(QiskitError):
         _ = SparsePauliOp.from_sparse_list([("ZZ", [0, 0], 1)], 2)
     with self.assertRaises(QiskitError):
         _ = SparsePauliOp.from_sparse_list([("ZI", [0, 0], 1)], 2)
     with self.assertRaises(QiskitError):
         _ = SparsePauliOp.from_sparse_list([("IZ", [0, 0], 1)], 2)
示例#8
0
 def test_enumerate(self):
     """Test enumerate with SparsePauliOp."""
     labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     table = PauliTable.from_labels(labels)
     op = SparsePauliOp(table, coeffs)
     for idx, i in enumerate(op):
         self.assertEqual(i, SparsePauliOp(labels[idx], coeffs[[idx]]))
示例#9
0
 def test_label_iter(self):
     """Test PauliTable label_iter method."""
     labels = ['III', 'IXI', 'IYY', 'YIZ', 'XYZ', 'III']
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     table = PauliTable.from_labels(labels)
     op = SparsePauliOp(table, coeffs)
     for idx, i in enumerate(op.label_iter()):
         self.assertEqual(i, (labels[idx], coeffs[idx]))
示例#10
0
 def test_iter(self):
     """Test iter with PauliTable."""
     labels = ['III', 'IXI', 'IYY', 'YIZ', 'XYZ', 'III']
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     table = PauliTable.from_labels(labels)
     op = SparsePauliOp(table, coeffs)
     for idx, i in enumerate(iter(op)):
         self.assertEqual(i, SparsePauliOp(labels[idx], coeffs[[idx]]))
示例#11
0
 def test_matrix_iter(self):
     """Test PauliTable dense matrix_iter method."""
     labels = ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     table = PauliTable.from_labels(labels)
     op = SparsePauliOp(table, coeffs)
     for idx, i in enumerate(op.matrix_iter()):
         self.assertTrue(
             np.array_equal(i, coeffs[idx] * pauli_mat(labels[idx])))
 def to_operator(self):
     """Test to_operator method."""
     labels = ["XI", "YZ", "YY", "ZZ"]
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     spp_op = SparsePauliOp(labels, coeffs)
     target = Operator(np.zeros((4, 4), dtype=complex))
     for coeff, label in zip(coeffs, labels):
         target = target + Operator(coeff * pauli_mat(label))
     self.assertEqual(spp_op.to_operator(), target)
 def to_matrix(self):
     """Test to_matrix method."""
     labels = ["XI", "YZ", "YY", "ZZ"]
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     spp_op = SparsePauliOp(labels, coeffs)
     target = np.zeros((4, 4), dtype=complex)
     for coeff, label in zip(coeffs, labels):
         target += coeff * pauli_mat(label)
     np.testing.assert_array_equal(spp_op.to_matrix(), target)
示例#14
0
 def test_matrix_iter_sparse(self):
     """Test PauliTable sparse matrix_iter method."""
     labels = ['III', 'IXI', 'IYY', 'YIZ', 'XYZ', 'III']
     coeffs = np.array([1, 2, 3, 4, 5, 6])
     table = PauliTable.from_labels(labels)
     op = SparsePauliOp(table, coeffs)
     for idx, i in enumerate(op.matrix_iter(sparse=True)):
         self.assertTrue(
             np.array_equal(i.toarray(), coeffs[idx] * pauli_mat(labels[idx])))
示例#15
0
 def to_operator(self):
     """Test to_operator method."""
     labels = ['XI', 'YZ', 'YY', 'ZZ']
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     spp_op = SparsePauliOp(PauliTable.from_labels(labels), coeffs)
     target = Operator(np.zeros((4, 4), dtype=complex))
     for coeff, label in zip(coeffs, labels):
         target = target + Operator(coeff * pauli_mat(label))
     self.assertEqual(spp_op.to_operator(), target)
示例#16
0
 def to_matrix(self):
     """Test to_matrix method."""
     labels = ['XI', 'YZ', 'YY', 'ZZ']
     coeffs = [-3, 4.4j, 0.2 - 0.1j, 66.12]
     spp_op = SparsePauliOp(PauliTable.from_labels(labels), coeffs)
     target = np.zeros((4, 4), dtype=complex)
     for coeff, label in zip(coeffs, labels):
         target += coeff * pauli_mat(label)
     self.assertTrue(np.array_equal(spp_op.to_matrix(), target))
 def test_chop(self):
     """Test chop, which individually truncates real and imaginary parts of the coeffs."""
     eps = 1e-10
     op = SparsePauliOp(
         ["XYZ", "ZII", "ZII", "YZY"],
         coeffs=[eps + 1j * eps, 1 + 1j * eps, eps + 1j, 1 + 1j])
     simplified = op.chop(tol=eps)
     expected_coeffs = [1, 1j, 1 + 1j]
     expected_paulis = ["ZII", "ZII", "YZY"]
     self.assertListEqual(simplified.coeffs.tolist(), expected_coeffs)
     self.assertListEqual(simplified.paulis.to_labels(), expected_paulis)
示例#18
0
 def test_simplify(self):
     """Test simplify method"""
     coeffs = [3 + 1j, -3 - 1j, 0, 4, -5, 2.2, -1.1j]
     labels = ["IXI", "IXI", "ZZZ", "III", "III", "XXX", "XXX"]
     spp_op = SparsePauliOp.from_list(zip(labels, coeffs))
     simplified_op = spp_op.simplify()
     target_coeffs = [-1, 2.2 - 1.1j]
     target_labels = ["III", "XXX"]
     target_op = SparsePauliOp.from_list(zip(target_labels, target_coeffs))
     self.assertEqual(simplified_op, target_op)
     np.testing.assert_array_equal(simplified_op.paulis.phase, np.zeros(simplified_op.size))
示例#19
0
 def test_simplify(self):
     """Test simplify method"""
     coeffs = [3 + 1j, -3 - 1j, 0, 4, -5, 2.2, -1.1j]
     labels = ['IXI', 'IXI', 'ZZZ', 'III', 'III', 'XXX', 'XXX']
     value = SparsePauliOp(
         PauliTable.from_labels(labels), coeffs).simplify()
     target_coeffs = [-1, 2.2 - 1.1j]
     target_labels = ['III', 'XXX']
     target = SparsePauliOp(
         PauliTable.from_labels(target_labels), target_coeffs)
     self.assertEqual(value, target)
示例#20
0
 def test_pauli_table_init(self):
     """Test PauliTable initialization."""
     labels = ['I', 'X', 'Y', 'Z']
     table = PauliTable.from_labels(labels)
     with self.subTest(msg='no coeffs'):
         spp_op = SparsePauliOp(table)
         self.assertTrue(np.array_equal(spp_op.coeffs, np.ones(len(labels))))
         self.assertEqual(spp_op.table, table)
     with self.subTest(msg='no coeffs'):
         coeffs = [1, 2, 3, 4]
         spp_op = SparsePauliOp(table, coeffs)
         self.assertTrue(np.array_equal(spp_op.coeffs, coeffs))
         self.assertEqual(spp_op.table, table)
    def test_eq_equiv(self):
        """Test __eq__ and equiv methods with some specific cases."""
        with self.subTest("shuffled"):
            spp_op1 = SparsePauliOp.from_list([("X", 1), ("Y", 2)])
            spp_op2 = SparsePauliOp.from_list([("Y", 2), ("X", 1)])
            self.assertNotEqual(spp_op1, spp_op2)
            self.assertTrue(spp_op1.equiv(spp_op2))

        with self.subTest("w/ zero"):
            spp_op1 = SparsePauliOp.from_list([("X", 1), ("Y", 1)])
            spp_op2 = SparsePauliOp.from_list([("X", 1), ("Y", 1), ("Z", 0)])
            self.assertNotEqual(spp_op1, spp_op2)
            self.assertTrue(spp_op1.equiv(spp_op2))
 def test_pauli_table_init(self):
     """Test PauliTable initialization."""
     labels = ["I", "X", "Y", "Z"]
     table = PauliTable.from_labels(labels)
     paulis = PauliList(labels)
     with self.subTest(msg="no coeffs"):
         spp_op = SparsePauliOp(table)
         np.testing.assert_array_equal(spp_op.coeffs, np.ones(len(labels)))
         self.assertEqual(spp_op.paulis, paulis)
     with self.subTest(msg="no coeffs"):
         coeffs = [1, 2, 3, 4]
         spp_op = SparsePauliOp(table, coeffs)
         np.testing.assert_array_equal(spp_op.coeffs, coeffs)
         self.assertEqual(spp_op.paulis, paulis)
示例#23
0
 def test_from_zip(self):
     """Test from_list method for zipped input."""
     labels = ['XXZ', 'IXI', 'YZZ', 'III']
     coeffs = [3.0, 5.5, -1j, 23.3333]
     spp_op = SparsePauliOp.from_list(zip(labels, coeffs))
     self.assertTrue(np.array_equal(spp_op.coeffs, coeffs))
     self.assertEqual(spp_op.table, PauliTable.from_labels(labels))
示例#24
0
 def test_str_init(self):
     """Test PauliTable initialization."""
     for label in ['IZ', 'XI', 'YX', 'ZZ']:
         table = PauliTable(label)
         spp_op = SparsePauliOp(label)
         self.assertEqual(spp_op.table, table)
         self.assertTrue(np.array_equal(spp_op.coeffs, [1]))
示例#25
0
 def random_spp_op(self, num_qubits, num_terms):
     """Generate a pseudo-random SparsePauliOp"""
     coeffs = (self.RNG.uniform(-1, 1, size=num_terms)
               + 1j * self.RNG.uniform(-1, 1, size=num_terms))
     labels = [''.join(self.RNG.choice(['I', 'X', 'Y', 'Z'], size=num_qubits))
               for _ in range(num_terms)]
     return SparsePauliOp(PauliTable.from_labels(labels), coeffs)
示例#26
0
def _pauli_id(n_qubits: int, coeff=None) -> SparsePauliOp:
    """Return the identity for `SparsePauliOp` on `n_qubits` qubits."""
    if coeff is None:
        coeff = complex(1.0)
    return SparsePauliOp(
        Pauli((np.zeros(n_qubits, dtype=bool), np.zeros(n_qubits,
                                                        dtype=bool))), [coeff])
 def test_from_zip(self):
     """Test from_list method for zipped input."""
     labels = ["XXZ", "IXI", "YZZ", "III"]
     coeffs = [3.0, 5.5, -1j, 23.3333]
     spp_op = SparsePauliOp.from_list(zip(labels, coeffs))
     np.testing.assert_array_equal(spp_op.coeffs, coeffs)
     self.assertEqual(spp_op.paulis, PauliList(labels))
示例#28
0
 def test_str_init(self):
     """Test PauliTable initialization."""
     for label in ["IZ", "XI", "YX", "ZZ"]:
         table = PauliTable(label)
         spp_op = SparsePauliOp(label)
         self.assertEqual(spp_op.table, table)
         self.assertTrue(np.array_equal(spp_op.coeffs, [1]))
示例#29
0
 def test_pauli_list_init(self):
     """Test PauliList initialization."""
     labels = ["I", "X", "Y", "-Z", "iZ", "-iX"]
     paulis = PauliList(labels)
     with self.subTest(msg="no coeffs"):
         spp_op = SparsePauliOp(paulis)
         np.testing.assert_array_equal(spp_op.coeffs, [1, 1, 1, -1, 1j, -1j])
         paulis.phase = 0
         self.assertEqual(spp_op.paulis, paulis)
     paulis = PauliList(labels)
     with self.subTest(msg="with coeffs"):
         coeffs = [1, 2, 3, 4, 5, 6]
         spp_op = SparsePauliOp(paulis, coeffs)
         np.testing.assert_array_equal(spp_op.coeffs, [1, 2, 3, -4, 5j, -6j])
         paulis.phase = 0
         self.assertEqual(spp_op.paulis, paulis)
 def test_str_init(self):
     """Test str initialization."""
     for label in ["IZ", "XI", "YX", "ZZ"]:
         pauli_list = PauliList(label)
         spp_op = SparsePauliOp(label)
         self.assertEqual(spp_op.paulis, pauli_list)
         np.testing.assert_array_equal(spp_op.coeffs, [1])