def trotter_circuit(self): '''Creates time evolution circuit with time as free parameter according the method specified by self.trot_options''' evo_H = self.evo_H # this may be different from self.H evo_time = Parameter('t') # evolution time, to be set later self.evo_time = evo_time evo_op = (evo_time * evo_H).exp_i() # evolution operator if self.trot_options['product_formula'] == 'lietrotter': num_reps = self.trot_options['reps'] trotterized_op = PauliTrotterEvolution(trotter_mode='trotter', reps=num_reps).convert(evo_op) qc = trotterized_op.to_circuit() elif self.trot_options['product_formula'] == 'suzuki': num_reps = self.trot_options['reps'] order = self.trot_options['order'] trotterized_op = PauliTrotterEvolution( trotter_mode=Suzuki(order=order, reps=num_reps)).convert(evo_op) qc = trotterized_op.to_circuit() return qc
def test_bind_parameter_list(self): """ bind parameters list test """ thetas = ParameterVector('θ', length=6) op = (thetas[1] * I ^ Z) + \ (thetas[2] * X ^ X) + \ (thetas[3] * Z ^ I) + \ (thetas[4] * Y ^ Z) + \ (thetas[5] * Z ^ Z) op = thetas[0] * op evolution = PauliTrotterEvolution(trotter_mode='trotter', reps=1) # wf = (Pl^Pl) + (Ze^Ze) wf = (op).exp_i() @ CX @ (H ^ I) @ Zero evo = evolution.convert(wf) param_list = np.transpose( [np.arange(10, 16), np.arange(2, 8), np.arange(30, 36)]).tolist() means = evo.assign_parameters({thetas: param_list}) self.assertIsInstance(means, ListOp) # Check that the no parameters are in the circuit for p in thetas[1:]: for circop in means.oplist: self.assertNotIn(p, circop.to_circuit().parameters) # Check that original circuit is unchanged for p in thetas: self.assertIn(p, evo.to_circuit().parameters)
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 test_bind_parameters(self): """ bind parameters test """ thetas = ParameterVector('θ', length=6) op = (thetas[1] * I ^ Z) + \ (thetas[2] * X ^ X) + \ (thetas[3] * Z ^ I) + \ (thetas[4] * Y ^ Z) + \ (thetas[5] * Z ^ Z) op = thetas[0] * op evolution = PauliTrotterEvolution(trotter_mode='trotter', reps=1) # wf = (Pl^Pl) + (Ze^Ze) wf = (op).exp_i() @ CX @ (H ^ I) @ Zero wf = wf.bind_parameters({thetas: np.arange(10, 16)}) mean = evolution.convert(wf) circuit_params = mean.to_circuit().parameters # Check that the no parameters are in the circuit for p in thetas[1:]: self.assertNotIn(p, circuit_params)
def test_parameterized_evolution(self): """ parameterized evolution test """ thetas = ParameterVector('θ', length=7) op = (thetas[0] * I ^ I) + \ (thetas[1] * I ^ Z) + \ (thetas[2] * X ^ X) + \ (thetas[3] * Z ^ I) + \ (thetas[4] * Y ^ Z) + \ (thetas[5] * Z ^ Z) op = op * thetas[6] evolution = PauliTrotterEvolution(trotter_mode='trotter', reps=1) # wf = (Pl^Pl) + (Ze^Ze) wf = (op).exp_i() @ CX @ (H ^ I) @ Zero mean = evolution.convert(wf) circuit_params = mean.to_circuit().parameters # Check that the non-identity parameters are in the circuit for p in thetas[1:]: self.assertIn(p, circuit_params) self.assertNotIn(thetas[0], circuit_params)
def test_parameterized_evolution(self): """ parameterized evolution test """ thetas = ParameterVector('θ', length=7) op = (thetas[0] * I ^ I) + \ (thetas[1] * I ^ Z) + \ (thetas[2] * X ^ X) + \ (thetas[3] * Z ^ I) + \ (thetas[4] * Y ^ Z) + \ (thetas[5] * Z ^ Z) op = op * thetas[6] evolution = PauliTrotterEvolution(trotter_mode='trotter', reps=1) # wf = (Pl^Pl) + (Ze^Ze) wf = (op).exp_i() @ CX @ (H ^ I) @ Zero mean = evolution.convert(wf) circuit = mean.to_circuit() # Check that all parameters are in the circuit for p in thetas: self.assertIn(p, circuit.parameters) # Check that the identity-parameters only exist as global phase self.assertNotIn(thetas[0], circuit._parameter_table.get_keys())
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)