def _optimise(self, ) -> list: # 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
def get_pq_command_from_tk_command(command: Command, engine: MainEngine, container): op = command.op optype = op.get_type() controlled = False if optype in _tk_to_pq_singleqs: gatetype = _tk_to_pq_singleqs[optype] elif optype in _tk_to_pq_multiqs: gatetype = _tk_to_pq_multiqs[optype] controlled = True else: raise Exception("Cannot convert op " + str(command) + " to projectq") # params = op.get_params() if issubclass(gatetype, pqo.BasicRotationGate): params = op.get_params() if len(params) != 1: raise Exception("A Rotation Gate has " + len(params) + " parameters") try: gate = gatetype(params[0].evalf() * PI) except: gate = gatetype(params[0] * PI) elif issubclass(gatetype, pqo.BasicGate): gate = gatetype() else: raise Exception("Gate of type: " + str(gatetype) + " cannot be converted") qubs = command.qubits if controlled: target = container[qubs[-1]] qubs.pop() controls = (container[i] for i in qubs) qubits = gate.make_tuple_of_qureg(target) cmd = ProjectQCommand(engine, gate, qubits, controls) else: qubits = gate.make_tuple_of_qureg(container[i] for i in qubs) cmd = ProjectQCommand(engine, gate, qubits) return cmd