def test_matrix_iter(self): """Test PauliSumOp 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) coeff = 10 op = PauliSumOp(SparsePauliOp(table, coeffs), coeff) for idx, i in enumerate(op.matrix_iter()): self.assertTrue(np.array_equal(i, coeff * coeffs[idx] * Pauli(labels[idx]).to_matrix()))
def test_matrix_iter_sparse(self): """Test PauliSumOp sparse matrix_iter method.""" labels = ['III', 'IXI', 'IYY', 'YIZ', 'XYZ', 'III'] coeffs = np.array([1, 2, 3, 4, 5, 6]) coeff = 10 table = PauliTable.from_labels(labels) op = PauliSumOp(SparsePauliOp(table, coeffs), coeff) for idx, i in enumerate(op.matrix_iter(sparse=True)): self.assertTrue( np.array_equal(i.toarray(), coeff * coeffs[idx] * Pauli(labels[idx]).to_matrix()))
def test_coder_operators(self): """Test runtime encoder and decoder for operators.""" x = Parameter("x") y = x + 1 qc = QuantumCircuit(1) qc.h(0) coeffs = np.array([1, 2, 3, 4, 5, 6]) table = PauliTable.from_labels( ["III", "IXI", "IYY", "YIZ", "XYZ", "III"]) op = (2.0 * I ^ I) z2_symmetries = Z2Symmetries( [Pauli("IIZI"), Pauli("ZIII")], [Pauli("IIXI"), Pauli("XIII")], [1, 3], [-1, 1]) isqrt2 = 1 / np.sqrt(2) sparse = scipy.sparse.csr_matrix([[0, isqrt2, 0, isqrt2]]) subtests = ( PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]), coeff=3), PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[1]), coeff=y), PauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[1 + 2j]), coeff=3 - 2j), PauliSumOp.from_list([("II", -1.052373245772859), ("IZ", 0.39793742484318045)]), PauliSumOp(SparsePauliOp(table, coeffs), coeff=10), MatrixOp(primitive=np.array([[0, -1j], [1j, 0]]), coeff=x), PauliOp(primitive=Pauli("Y"), coeff=x), CircuitOp(qc, coeff=x), EvolvedOp(op, coeff=x), TaperedPauliSumOp(SparsePauliOp(Pauli("XYZX"), coeffs=[2]), z2_symmetries), StateFn(qc, coeff=x), CircuitStateFn(qc, is_measurement=True), DictStateFn("1" * 3, is_measurement=True), VectorStateFn(np.ones(2**3, dtype=complex)), OperatorStateFn(CircuitOp(QuantumCircuit(1))), SparseVectorStateFn(sparse), Statevector([1, 0]), CVaRMeasurement(Z, 0.2), ComposedOp([(X ^ Y ^ Z), (Z ^ X ^ Y ^ Z).to_matrix_op()]), SummedOp([X ^ X * 2, Y ^ Y], 2), TensoredOp([(X ^ Y), (Z ^ I)]), (Z ^ Z) ^ (I ^ 2), ) for op in subtests: with self.subTest(op=op): encoded = json.dumps(op, cls=RuntimeEncoder) self.assertIsInstance(encoded, str) decoded = json.loads(encoded, cls=RuntimeDecoder) self.assertEqual(op, decoded)
def _fix_qubits( operator: Union[int, PauliSumOp, PauliOp, OperatorBase], has_side_chain_second_bead: bool = False, ) -> Union[int, PauliSumOp, PauliOp, OperatorBase]: """ Assigns predefined values for turns qubits on positions 0, 1, 2, 3, 5 in the main chain without the loss of generality (see the paper https://arxiv.org/pdf/1908.02163.pdf). Qubits on these position are considered fixed and not subject to optimization. Args: operator: an operator whose qubits shall be fixed. Returns: An operator with relevant qubits changed to fixed values. """ # operator might be 0 (int) because it is initialized as operator = 0; then we should not # attempt fixing qubits if (not isinstance(operator, PauliOp) and not isinstance(operator, PauliSumOp) and not isinstance(operator, OperatorBase)): return operator operator = operator.reduce() new_tables = [] new_coeffs = [] if isinstance(operator, PauliOp): table_z = np.copy(operator.primitive.z) table_x = np.copy(operator.primitive.x) _preset_binary_vals(table_z, has_side_chain_second_bead) return PauliOp(Pauli((table_z, table_x))) for hamiltonian in operator: table_z = np.copy(hamiltonian.primitive.paulis.z[0]) table_x = np.copy(hamiltonian.primitive.paulis.x[0]) coeffs = _calc_updated_coeffs(hamiltonian, table_z, has_side_chain_second_bead) _preset_binary_vals(table_z, has_side_chain_second_bead) new_table = np.concatenate((table_x, table_z), axis=0) new_tables.append(new_table) new_coeffs.append(coeffs) new_pauli_table = PauliTable(data=new_tables) operator_updated = PauliSumOp( SparsePauliOp(data=new_pauli_table, coeffs=new_coeffs)) operator_updated = operator_updated.reduce() return operator_updated
def _compress_pauli_sum_op( num_qubits: int, total_hamiltonian: Union[PauliSumOp, PauliOp, OperatorBase], unused_qubits: List[int], ) -> Union[PauliSumOp, PauliOp, OperatorBase]: new_tables = [] new_coeffs = [] for term in total_hamiltonian: table_z = term.primitive.paulis.z[0] table_x = term.primitive.paulis.x[0] coeffs = term.primitive.coeffs[0] new_table_z, new_table_x = _calc_reduced_pauli_tables( num_qubits, table_x, table_z, unused_qubits) new_table = np.concatenate((new_table_x, new_table_z), axis=0) new_tables.append(new_table) new_coeffs.append(coeffs) new_pauli_table = PauliTable(data=new_tables) total_hamiltonian_compressed = PauliSumOp( SparsePauliOp(data=new_pauli_table, coeffs=new_coeffs)).reduce() return total_hamiltonian_compressed