Ejemplo n.º 1
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)
Ejemplo n.º 2
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))
Ejemplo n.º 3
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)
Ejemplo n.º 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)
Ejemplo n.º 5
0
def _bloch_multivector_data(state):
    """Return list of bloch vectors for each qubit

    Args:
        state (DensityMatrix or Statevector): an N-qubit state.

    Returns:
        list: list of bloch vectors (x, y, z) for each qubit.

    Raises:
        VisualizationError: if input is not an N-qubit state.
    """
    rho = DensityMatrix(state)
    num = rho.num_qubits
    if num is None:
        raise VisualizationError("Input is not a multi-qubit quantum state.")
    pauli_singles = PauliTable.from_labels(['X', 'Y', 'Z'])
    bloch_data = []
    for i in range(num):
        if num > 1:
            paulis = PauliTable(np.zeros((3, 2 * (num-1)), dtype=np.bool)).insert(
                i, pauli_singles, qubit=True)
        else:
            paulis = pauli_singles
        bloch_state = [np.real(np.trace(np.dot(mat, rho.data))) for mat in paulis.matrix_iter()]
        bloch_data.append(bloch_state)
    return bloch_data
Ejemplo n.º 6
0
 def test_from_list(self):
     """Test from_list method."""
     labels = ["XXZ", "IXI", "YZZ", "III"]
     coeffs = [3.0, 5.5, -1j, 23.3333]
     spp_op = SparsePauliOp.from_list(list(zip(labels, coeffs)))
     self.assertTrue(np.array_equal(spp_op.coeffs, coeffs))
     self.assertEqual(spp_op.table, PauliTable.from_labels(labels))
Ejemplo n.º 7
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]))
Ejemplo n.º 8
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]]))
Ejemplo n.º 9
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]]))
Ejemplo n.º 10
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])))
Ejemplo n.º 11
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)
Ejemplo n.º 12
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))
Ejemplo n.º 13
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])))
Ejemplo n.º 14
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_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)