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
def _remove_identity_from_idle( circuit: qiskit.QuantumCircuit, idle_indices: Set[int], ) -> None: """Removes identities from the circuit corresponding to the set of indices. Used in conjunction with _add_identity_to_idle to preserve idle qubits and indices in conversion. Args: circuit: Qiskit circuit to have identities removed idle_indices: Set of altered idle qubit indices """ index_list: List[int] = [] data = copy.deepcopy(circuit._data) for target_index, op in enumerate(data): bit_indices = set() gate, qubits, cbits = op bit_indices.update(set(bit.index for bit in qubits)) if gate.name == "id" and bit_indices.intersection(idle_indices): # Reverse index list order for data index preservation index_list.insert(0, target_index) # Traverse data from list end to preserve index for target_index in index_list: del data[target_index] circuit._data = data
def repeat(self, n): """Creates an instruction with `gate` repeated `n` amount of times. Args: n (int): Number of times to repeat the instruction Returns: qiskit.circuit.Instruction: Containing the definition. Raises: CircuitError: If n < 1. """ if int(n) != n or n < 1: raise CircuitError( "Repeat can only be called with strictly positive integer.") n = int(n) instruction = self._return_repeat(n) qargs = [] if self.num_qubits == 0 else QuantumRegister( self.num_qubits, 'q') cargs = [] if self.num_clbits == 0 else ClassicalRegister( self.num_clbits, 'c') if instruction.definition is None: # pylint: disable=cyclic-import from qiskit import QuantumCircuit qc = QuantumCircuit() if qargs: qc.add_register(qargs) if cargs: qc.add_register(cargs) qc._data = [(self, qargs[:], cargs[:])] * n instruction.definition = qc return instruction
def _define(self): rule = [] # type: List[Tuple[Gate, list, list]] q = QuantumRegister(len(self.a_01s) + len(self.b_01s), "q") qc = QuantumCircuit(q, name=self.name) a = q[0:len(self.a_01s)] b = q[len(self.a_01s):] for i, c in enumerate(self.a_01s): if c == '1': rule.append((XGate(), [a[i]], [])) for i, c in enumerate(self.b_01s): if c == '1': rule.append((XGate(), [b[i]], [])) rule.append((QuantumFourierTransformGate(len(a)), a, [])) for b_index in reversed(range(len(b))): theta_index = 1 for a_index in reversed(range(b_index + 1)): rule.append((CU1Gate(qft.get_theta(theta_index)), [b[b_index], a[a_index]], [])) theta_index += 1 rule.append((QuantumFourierTransformGate(len(a)).inverse(), a, [])) qc._data = rule.copy() self.definition = qc
def _define(self): q = QuantumRegister(self.num_qubits, "q") qc = QuantumCircuit(q, name=self.name) rule = [] # type: List[Tuple[Gate, List[Qubit], List[Clbit]]] length = 2**self.control_qubits alpha = sparse.dok_matrix((length, 1), dtype=np.float64) alpha[self.conditional_case] = np.pi rule.append((HGate(), [q[-1]], [])) rule.append((UniformRotationGate(RYGate, alpha), list(q), [])) rule.append((HGate(), [q[-1]], [])) qc._data = rule.copy() self.definition = qc
def _define(self): nqubits = self.num_qubits q = QuantumRegister(nqubits, 'q') qc = QuantumCircuit(q, name=self.name) theta = self.params[:nqubits] # parameters of odd block phi = self.params[nqubits:] # parameters of even block rules = [(OddBlock(nqubits, theta), q[:], []), (EvenBlock(nqubits, phi), q[:], [])] qc._data = rules self.definition = qc
def _define(self): rule = [] # type: List[Tuple[Gate, List[Qubit], List[Clbit]]] qreg = QuantumRegister(self.num_qubits, "qreg") qc = QuantumCircuit(qreg, name=self.name) q_list = list(qreg) unused = q_list.copy() for qr in q_list: rule.append((HGate(), [qr], [])) k = 2 unused.remove(qr) for qj in unused: rule.append((CU1Gate(get_theta(k)), [qj, qr], [])) k = k + 1 qc._data = rule.copy() self.definition = qc