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])
 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))
Ejemplo n.º 3
0
 def test_from_operator(self):
     """Test from_operator methods."""
     for tup in it.product(["I", "X", "Y", "Z"], repeat=2):
         label = "".join(tup)
         with self.subTest(msg=label):
             spp_op = SparsePauliOp.from_operator(Operator(pauli_mat(label)))
             np.testing.assert_array_equal(spp_op.coeffs, [1])
             self.assertEqual(spp_op.paulis, PauliList(label))
Ejemplo n.º 4
0
 def test_from_index_list(self):
     """Test from_list method specifying the Paulis via indices."""
     expected_labels = ["XXZ", "IXI", "YIZ", "III"]
     paulis = ["XXZ", "X", "YZ", ""]
     indices = [[2, 1, 0], [1], [2, 0], []]
     coeffs = [3.0, 5.5, -1j, 23.3333]
     spp_op = SparsePauliOp.from_sparse_list(zip(paulis, indices, coeffs), num_qubits=3)
     np.testing.assert_array_equal(spp_op.coeffs, coeffs)
     self.assertEqual(spp_op.paulis, PauliList(expected_labels))
 def test_sparse_pauli_op_init(self):
     """Test SparsePauliOp initialization."""
     labels = ["I", "X", "Y", "-Z", "iZ", "-iX"]
     with self.subTest(msg="make SparsePauliOp from SparsePauliOp"):
         op = SparsePauliOp(labels)
         ref_op = op.copy()
         spp_op = SparsePauliOp(op)
         self.assertEqual(spp_op, ref_op)
         np.testing.assert_array_equal(ref_op.paulis.phase,
                                       np.zeros(ref_op.size))
         np.testing.assert_array_equal(spp_op.paulis.phase,
                                       np.zeros(spp_op.size))
         # make sure the changes of `op` do not propagate through to `spp_op`
         op.paulis.z[:] = False
         op.coeffs *= 2
         self.assertNotEqual(spp_op, op)
         self.assertEqual(spp_op, ref_op)
     with self.subTest(
             msg="make SparsePauliOp from SparsePauliOp and ndarray"):
         op = SparsePauliOp(labels)
         coeffs = np.array([1, 2, 3, 4, 5, 6])
         spp_op = SparsePauliOp(op, coeffs)
         ref_op = SparsePauliOp(op.paulis.copy(), coeffs.copy())
         self.assertEqual(spp_op, ref_op)
         np.testing.assert_array_equal(ref_op.paulis.phase,
                                       np.zeros(ref_op.size))
         np.testing.assert_array_equal(spp_op.paulis.phase,
                                       np.zeros(spp_op.size))
         # make sure the changes of `op` and `coeffs` do not propagate through to `spp_op`
         op.paulis.z[:] = False
         coeffs *= 2
         self.assertNotEqual(spp_op, op)
         self.assertEqual(spp_op, ref_op)
     with self.subTest(msg="make SparsePauliOp from PauliList"):
         paulis = PauliList(labels)
         spp_op = SparsePauliOp(paulis)
         ref_op = SparsePauliOp(labels)
         self.assertEqual(spp_op, ref_op)
         np.testing.assert_array_equal(ref_op.paulis.phase,
                                       np.zeros(ref_op.size))
         np.testing.assert_array_equal(spp_op.paulis.phase,
                                       np.zeros(spp_op.size))
         # make sure the change of `paulis` does not propagate through to `spp_op`
         paulis.z[:] = False
         self.assertEqual(spp_op, ref_op)
     with self.subTest(msg="make SparsePauliOp from PauliList and ndarray"):
         paulis = PauliList(labels)
         coeffs = np.array([1, 2, 3, 4, 5, 6])
         spp_op = SparsePauliOp(paulis, coeffs)
         ref_op = SparsePauliOp(labels, coeffs.copy())
         self.assertEqual(spp_op, ref_op)
         np.testing.assert_array_equal(ref_op.paulis.phase,
                                       np.zeros(ref_op.size))
         np.testing.assert_array_equal(spp_op.paulis.phase,
                                       np.zeros(spp_op.size))
         # make sure the changes of `paulis` and `coeffs` do not propagate through to `spp_op`
         paulis.z[:] = False
         coeffs[:] = 0
         self.assertEqual(spp_op, ref_op)
 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)
Ejemplo n.º 7
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)