def _transform_registers( circuit: qiskit.QuantumCircuit, new_qregs: Optional[List[qiskit.QuantumRegister]] = None, ) -> None: """Transforms the registers in the circuit to the new registers. Args: circuit: Qiskit circuit with at most one quantum register. new_qregs: The new quantum registers for the circuit. Raises: ValueError: * If the input circuit has more than one quantum register. * If the number of qubits in the new quantum registers is greater than the number of qubits in the circuit. """ if new_qregs is None: return if len(circuit.qregs) > 1: raise ValueError( "Input circuit is required to have <= 1 quantum register but has " f"{len(circuit.qregs)} quantum registers." ) qreg_sizes = [qreg.size for qreg in new_qregs] nqubits_in_circuit = circuit.num_qubits if len(qreg_sizes) and sum(qreg_sizes) < nqubits_in_circuit: raise ValueError( f"The circuit has {nqubits_in_circuit} qubit(s), but the provided " f"quantum registers have {sum(qreg_sizes)} qubit(s)." ) # Copy the circuit data. data = copy.deepcopy(circuit._data) # Remove the old qubits and add the new ones. circuit._qubits = [] circuit._qubit_set = set() circuit.qregs = [] circuit._data = [] circuit._qubit_indices = {} circuit.add_register(*new_qregs) # Map the qubits in operations to the new qubits. new_ops = [] for op in data: gate, qubits, cbits = op new_qubits = _map_qubits(qubits, qreg_sizes, new_qregs) new_ops.append((gate, new_qubits, cbits)) circuit._data = new_ops