def get_state(self,circuit:Circuit, fit_to_constraints=True) -> list: c = circuit.copy() if fit_to_constraints : Transform.RebaseToProjectQ().apply(c) fwd = ForwarderEngine(self._backend) eng = MainEngine(backend=self._backend,engine_list=[fwd]) qureg = eng.allocate_qureg(c.n_qubits) tk_to_projectq(eng,qureg,c) eng.flush() state = self._backend.cheat()[1] #`cheat()` returns tuple:(a dictionary of qubit indices, statevector) All(Measure) | qureg return state #list of complex numbers
def projectq_expectation_value(circuit:Circuit,hamiltonian:QubitOperator) -> float : ProjectQback = Simulator() fwd = ForwarderEngine(ProjectQback) eng = MainEngine(backend=ProjectQback,engine_list=[fwd]) qureg = eng.allocate_qureg(circuit.n_qubits) c = circuit.copy() Transform.RebaseToProjectQ().apply(c) tk_to_projectq(eng,qureg,c) eng.flush() energy = eng.backend.get_expectation_value(hamiltonian,qureg) All(Measure) | qureg return energy
def _optimise(self): #takes the circuit and optimises it before regurgitating it as a series of ProjectQ commands if self._circuit.n_qubits!=0: Transform.OptimisePhaseGadgets().apply(self._circuit) Transform.RebaseToProjectQ().apply(self._circuit) cmd_list = [] for i in range(self._circuit.n_qubits): gate = pqo.Allocate cmd = ProjectQCommand(self.main_engine,gate,gate.make_tuple_of_qureg(self._qubit_dictionary[i])) cmd_list.append(cmd) if self._circuit.n_gates==0: return cmd_list for command in self._circuit: cmd = _get_pq_command_from_tk_command(command,self.main_engine,self._qubit_dictionary) cmd_list.append(cmd) return cmd_list