def trotter_circ(q, exp_args, n_steps): qc = QuantumCircuit(q) for i in range(n_steps): for sub_op in exp_args: qc += PauliTrotterEvolution().convert( EvolvedOp(sub_op)).to_circuit() return qc
def exp_i(self) -> OperatorBase: """ Return an ``OperatorBase`` equivalent to an exponentiation of self * -i, e^(-i*op).""" # pylint: disable=unidiomatic-typecheck if type(self) == ListOp: return ListOp([op.exp_i() for op in self.oplist], coeff=self.coeff) # pylint: disable=import-outside-toplevel from qiskit.aqua.operators import EvolvedOp return EvolvedOp(self)
def test_trotter_with_identity(self): """ trotterization of operator with identity term """ op = (2.0 * I ^ I) + (Z ^ Y) exact_matrix = scipy.linalg.expm(-1j * op.to_matrix()) evo = PauliTrotterEvolution(trotter_mode='suzuki', reps=2) with self.subTest('all PauliOp terms'): circ_op = evo.convert(EvolvedOp(op)) circuit_matrix = qiskit.quantum_info.Operator(circ_op.to_circuit()).data np.testing.assert_array_almost_equal(exact_matrix, circuit_matrix) with self.subTest('MatrixOp identity term'): op = (2.0 * I ^ I).to_matrix_op() + (Z ^ Y) circ_op = evo.convert(EvolvedOp(op)) circuit_matrix = qiskit.quantum_info.Operator(circ_op.to_circuit()).data np.testing.assert_array_almost_equal(exact_matrix, circuit_matrix) with self.subTest('CircuitOp identity term'): op = (2.0 * I ^ I).to_circuit_op() + (Z ^ Y) circ_op = evo.convert(EvolvedOp(op)) circuit_matrix = qiskit.quantum_info.Operator(circ_op.to_circuit()).data np.testing.assert_array_almost_equal(exact_matrix, circuit_matrix)
def exp_i(self) -> OperatorBase: """ Return Operator exponentiation, equaling e^(-i * op)""" # pylint: disable=cyclic-import,import-outside-toplevel from qiskit.aqua.operators import EvolvedOp return EvolvedOp(self)
def test_compose_with_indices(self): """ Test compose method using its permutation feature.""" pauli_op = (X ^ Y ^ Z) circuit_op = (T ^ H) matrix_op = (X ^ Y ^ H ^ T).to_matrix_op() evolved_op = EvolvedOp(matrix_op) # composition of PrimitiveOps num_qubits = 4 primitive_op = pauli_op @ circuit_op @ matrix_op composed_op = pauli_op @ circuit_op @ evolved_op self.assertEqual(primitive_op.num_qubits, num_qubits) self.assertEqual(composed_op.num_qubits, num_qubits) # with permutation num_qubits = 5 indices = [1, 4] permuted_primitive_op = evolved_op @ circuit_op.permute(indices) @ pauli_op @ matrix_op composed_primitive_op = \ evolved_op @ pauli_op.compose(circuit_op, permutation=indices, front=True) @ matrix_op self.assertTrue(np.allclose(permuted_primitive_op.to_matrix(), composed_primitive_op.to_matrix())) self.assertEqual(num_qubits, permuted_primitive_op.num_qubits) # ListOp num_qubits = 6 tensored_op = TensoredOp([pauli_op, circuit_op]) summed_op = pauli_op + circuit_op.permute([2, 1]) composed_op = circuit_op @ evolved_op @ matrix_op list_op = summed_op @ composed_op.compose(tensored_op, permutation=[1, 2, 3, 5, 4], front=True) self.assertEqual(num_qubits, list_op.num_qubits) num_qubits = 4 circuit_fn = CircuitStateFn(primitive=circuit_op.primitive, is_measurement=True) operator_fn = OperatorStateFn(primitive=circuit_op ^ circuit_op, is_measurement=True) no_perm_op = circuit_fn @ operator_fn self.assertEqual(no_perm_op.num_qubits, num_qubits) indices = [0, 4] perm_op = operator_fn.compose(circuit_fn, permutation=indices, front=True) self.assertEqual(perm_op.num_qubits, max(indices) + 1) # StateFn num_qubits = 3 dim = 2**num_qubits vec = [1.0/(i+1) for i in range(dim)] dic = {format(i, 'b').zfill(num_qubits): 1.0/(i+1) for i in range(dim)} is_measurement = True op_state_fn = OperatorStateFn(matrix_op, is_measurement=is_measurement) # num_qubit = 4 vec_state_fn = VectorStateFn(vec, is_measurement=is_measurement) # 3 dic_state_fn = DictStateFn(dic, is_measurement=is_measurement) # 3 circ_state_fn = CircuitStateFn(circuit_op.to_circuit(), is_measurement=is_measurement) # 2 composed_op = op_state_fn @ vec_state_fn @ dic_state_fn @ circ_state_fn self.assertEqual(composed_op.num_qubits, op_state_fn.num_qubits) # with permutation perm = [2, 4, 6] composed = \ op_state_fn @ dic_state_fn.compose(vec_state_fn, permutation=perm, front=True) @ \ circ_state_fn self.assertEqual(composed.num_qubits, max(perm) + 1)
def exp_i(self) -> OperatorBase: """ Return an ``OperatorBase`` equivalent to an exponentiation of self * -i, e^(-i*op).""" # pylint: disable=import-outside-toplevel from qiskit.aqua.operators import EvolvedOp return EvolvedOp(self)