Source code for qrisp.environments.gate_wrap_environment
"""\********************************************************************************* Copyright (c) 2025 the Qrisp authors** This program and the accompanying materials are made available under the* terms of the Eclipse Public License 2.0 which is available at* http://www.eclipse.org/legal/epl-2.0.** This Source Code may also be made available under the following Secondary* Licenses when the conditions for such availability set forth in the Eclipse* Public License, v. 2.0 are satisfied: GNU General Public License, version 2* with the GNU Classpath Exception which is* available at https://www.gnu.org/software/classpath/license.html.** SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0********************************************************************************/"""fromqrisp.circuitimportQuantumCircuit,QubitAlloc,QubitDealloc,XGatefromqrisp.environmentsimportQuantumEnvironment
[docs]classGateWrapEnvironment(QuantumEnvironment):""" This environment allows to hide complexity in the circuit visualisation. Operations appended inside this environment are bundled into a single :ref:`Instruction` object. The functionality of this :ref:`QuantumEnvironment` can also be used with the :meth:`gate_wrap <qrisp.gate_wrap>` decorator. After compiling, the wrapped instruction can be retrieved using the ``.instruction`` attribute. Parameters ---------- name : string, optional The name of the resulting gate. The default is None. Examples -------- We create some :ref:`QuantumVariable` and execute some gates inside a GateWrapEnvironment: :: from qrisp import QuantumVariable, GateWrapEnvironment, x, y, z qv = QuantumVariable(3) gwe = GateWrapEnvironment("example") with gwe: x(qv[0]) y(qv[1]) z(qv[2]) >>> print(qv.qs) :: QuantumCircuit: -------------- ┌──────────┐ qv.0: ┤0 ├ │ example │ qv.1: ┤1 ├ └──┬───┬───┘ qv.2: ───┤ Z ├──── └───┘ Live QuantumVariables: --------------------- QuantumVariable qv We can access the instruction, which has been appended using the ``.instruction`` attribute: >>> instruction = gwe.instruction >>> print(instruction.op.definition) :: ┌───┐ qb_41: ┤ X ├ ├───┤ qb_42: ┤ Y ├ └───┘ Using the :meth:`gate_wrap <qrisp.gate_wrap>` decorator we can quickly gate wrap functions: :: from qrisp import gate_wrap @gate_wrap def example_function(qv): x(qv[0]) y(qv[1]) z(qv[2]) example_function(qv) >>> print(qv.qs) :: QuantumCircuit: -------------- ┌──────────┐┌───────────────────┐ qv.0: ┤0 ├┤0 ├ │ example ││ │ qv.1: ┤1 ├┤1 example_function ├ └──┬───┬───┘│ │ qv.2: ───┤ Z ├────┤2 ├ └───┘ └───────────────────┘ Live QuantumVariables: --------------------- QuantumVariable qv """def__init__(self,name=None):super().__init__()self.name=nameself.manual_allocation_management=Truedefcompile(self):temp_data_list=list(self.env_qs.data)self.env_qs.data=[]super().compile()compiled_qc=self.env_qs.clearcopy()compiled_qc.data=list(self.env_qs.data)self.env_qs.clear_data()self.env_qs.data.extend(temp_data_list)iflen(compiled_qc.data)==0:self.instruction=NonereturnNoneqc=QuantumCircuit(len(self.env_qs.qubits),len(self.env_qs.clbits))translation_dic={self.env_qs.qubits[i]:qc.qubits[i]foriinrange(len(qc.qubits))}translation_dic.update({self.env_qs.clbits[i]:qc.clbits[i]foriinrange(len(qc.clbits))})qubit_set=set([])dealloc_list=[]alloc_list=[]forinstrincompiled_qc.data:qubit_set=qubit_set.union(set([translation_dic[qb]forqbininstr.qubits]))ifinstr.op.name=="qb_dealloc":instr.qubits[0].allocated=True# dealloc_list.append(instr)dealloc_list.append(instr.qubits[0])continueifinstr.op.name=="qb_alloc":alloc_list.append(instr.qubits[0])try:dealloc_list.remove(instr.qubits[0])exceptValueError:passcontinueqc.append(instr.op,[translation_dic[qb]forqbininstr.qubits],[translation_dic[cb]forcbininstr.clbits],)idle_qubit_list=list(set(qc.qubits)-qubit_set)forjinrange(len(idle_qubit_list)):foriinrange(len(qc.qubits)):ifqc.qubits[i].identifier==idle_qubit_list[j].identifier:qc.qubits.pop(i)breaktranslation_dic_inv={translation_dic[key]:keyforkeyintranslation_dic.keys()}gate=qc.to_gate(self.name)alloc_list=list(set(alloc_list))forqbinalloc_list:self.env_qs.append(QubitAlloc(),[qb])self.env_qs.append(gate,[translation_dic_inv[qb]forqbinqc.qubits],[translation_dic_inv[cb]forcbinqc.clbits],)self.instruction=self.env_qs.data[-1]dealloc_list=list(set(dealloc_list))forqbindealloc_list:self.env_qs.append(QubitDealloc(),[qb])qb.allocated=False
Get in touch!
If you are interested in Qrisp or high-level quantum algorithm research in general connect with us on our
Slack workspace.