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))) self.assertTrue(np.array_equal(spp_op.coeffs, [1])) self.assertEqual(spp_op.table, PauliTable(label))
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))
def _paulivec_data(state): """Return paulivec data for plotting. Args: state (DensityMatrix or Statevector): an N-qubit state. Returns: tuple: (labels, values) for Pauli vec. Raises: VisualizationError: if input is not an N-qubit state. """ rho = SparsePauliOp.from_operator(DensityMatrix(state)) if rho.num_qubits is None: raise VisualizationError("Input is not a multi-qubit quantum state.") return rho.table.to_labels(), np.real(rho.coeffs)
def _logarithmic_encoding( self, spin: Union[Fraction, int] ) -> Tuple[PauliSumOp, PauliSumOp, PauliSumOp, PauliSumOp]: """The logarithmic encoding. Args: spin: Positive half-integer (integer or half-odd-integer) that represents spin. Returns: A tuple containing four PauliSumOp. """ spin_op_encoding: List[PauliSumOp] = [] dspin = int(2 * spin + 1) num_qubits = int(np.ceil(np.log2(dspin))) # Get the spin matrices spin_matrices = [ SpinOp(symbol, spin=spin).to_matrix() for symbol in "XYZ" ] # Append the identity spin_matrices.append(np.eye(dspin)) # Embed the spin matrices in a larger matrix of size 2**num_qubits x 2**num_qubits embedded_spin_matrices = [ self._embed_matrix(matrix, num_qubits) for matrix in spin_matrices ] # Generate operators from these embedded spin matrices embedded_operators = [ Operator(matrix) for matrix in embedded_spin_matrices ] for op in embedded_operators: op = SparsePauliOp.from_operator(op) op.chop() spin_op_encoding.append(PauliSumOp(1.0 * op)) return tuple(spin_op_encoding) # type: ignore