def solve(self, output_mode=None, output_file_name=None): result_depth, list_scheduled_gate_name, list_scheduled_gate_qubits, final_mapping = super( ).solve("IR") circuit = Circuit() for t in range(result_depth): moment = Moment() for i, qubits in enumerate(list_scheduled_gate_qubits[t]): if isinstance(qubits, int): moment = moment.with_operation( GateOperation(list_scheduled_gate_name[t][i], (self.map_to_cirq_qubit[qubits], ))) else: if list_scheduled_gate_name[t][i] == "SWAP": moment = moment.with_operation( GateOperation(SWAP, (self.map_to_cirq_qubit[qubits[0]], self.map_to_cirq_qubit[qubits[1]]))) else: moment = moment.with_operation( GateOperation(list_scheduled_gate_name[t][i], (self.map_to_cirq_qubit[qubits[0]], self.map_to_cirq_qubit[qubits[1]]))) circuit += moment final_cirq_mapping = dict() for i in range(self.count_program_qubit): final_cirq_mapping[self.map_to_cirq_qubit[ final_mapping[i]]] = self.map_program_qubit_to[i] return [circuit, final_cirq_mapping]
def can_add_operation_into_moment(self, operation: cirq.Operation, moment: cirq.Moment) -> bool: """Determines if it's possible to add an operation into a moment. An operation can be added if the moment with the operation added is valid. Args: operation: The operation being added. moment: The moment being transformed. Returns: Whether or not the moment will validate after adding the operation. Raises: ValueError: If either of the given moment or operation is invalid """ if not super().can_add_operation_into_moment(operation, moment): return False try: self.validate_moment(moment.with_operation(operation)) except ValueError: return False return True