def test_pauli_evolution(self): """pauli evolution test""" op = ((-1.052373245772859 * I ^ I) + (0.39793742484318045 * I ^ Z) + (0.18093119978423156 * X ^ X) + (-0.39793742484318045 * Z ^ I) + (-0.01128010425623538 * Z ^ Z)) evolution = EvolutionFactory.build(operator=op) # wf = (Pl^Pl) + (Ze^Ze) wf = ((np.pi / 2) * op).exp_i() @ CX @ (H ^ I) @ Zero mean = evolution.convert(wf) self.assertIsNotNone(mean)
def construct_circuit(self, parameters, q=None): """ construct circuit """ if not len(parameters) == self.num_parameters: raise ValueError( 'Incorrect number of angles: expecting {}, but {} given.'. format(self.num_parameters, len(parameters))) # initialize circuit, possibly based on given register/initial state if isinstance(self._initial_state, QuantumCircuit): circuit_op = CircuitStateFn(self._initial_state) elif self._initial_state is not None: circuit_op = CircuitStateFn( self._initial_state.construct_circuit('circuit')) else: circuit_op = (H ^ self._num_qubits) # iterate over layers for idx in range(self._p): # the first [:self._p] parameters are used for the cost operator, # so we apply them here circuit_op = (self._cost_operator * parameters[idx]).exp_i().compose(circuit_op) if isinstance(self._mixer, OperatorBase): mixer = cast(OperatorBase, self._mixer) # we apply beta parameter in case of operator based mixer. circuit_op = ( mixer * parameters[idx + self._p]).exp_i().compose(circuit_op) else: # mixer as a quantum circuit that can be parameterized mixer = cast(QuantumCircuit, self._mixer) num_params = mixer.num_parameters # the remaining [self._p:] parameters are used for the mixer, # there may be multiple layers, so parameters are grouped by layers. param_values = parameters[self._p + num_params * idx:self._p + num_params * (idx + 1)] param_dict = dict(zip(mixer.parameters, param_values)) mixer = mixer.assign_parameters(param_dict) circuit_op = CircuitOp(mixer).compose(circuit_op) evolution = EvolutionFactory.build(self._cost_operator) circuit_op = evolution.convert(circuit_op) return circuit_op.to_circuit()
def _construct_circuit(self, parameters) -> QuantumCircuit: """Construct a parameterized circuit.""" if not len(parameters) == self._num_parameters: raise ValueError( 'Incorrect number of angles: expecting {}, but {} given.'. format(self._num_parameters, len(parameters))) # local imports to avoid circular imports from qiskit.opflow import CircuitStateFn from qiskit.opflow import CircuitOp, EvolutionFactory from qiskit.opflow import OperatorBase circuit_op = CircuitStateFn(self.initial_state) # iterate over layers for idx in range(self._reps): # the first [:self._reps] parameters are used for the cost operator, # so we apply them here circuit_op = (self._cost_operator * parameters[idx]).exp_i().compose(circuit_op) mixer = self.mixer_operator if isinstance(mixer, OperatorBase): mixer = cast(OperatorBase, mixer) # we apply beta parameter in case of operator based mixer. circuit_op = ( mixer * parameters[idx + self._reps]).exp_i().compose(circuit_op) else: # mixer as a quantum circuit that can be parameterized mixer = cast(QuantumCircuit, mixer) num_params = mixer.num_parameters # the remaining [self._p:] parameters are used for the mixer, # there may be multiple layers, so parameters are grouped by layers. param_values = parameters[self._reps + num_params * idx:self._reps + num_params * (idx + 1)] param_dict = dict(zip(mixer.parameters, param_values)) mixer = mixer.assign_parameters(param_dict) circuit_op = CircuitOp(mixer).compose(circuit_op) evolution = EvolutionFactory.build(self._cost_operator) circuit_op = evolution.convert(circuit_op) return circuit_op.to_circuit()