GateWrapEnvironment#

class GateWrapEnvironment(name=None)[source]#

This environment allows to hide complexity in the circuit visualisation. Operations appended inside this environment are bundled into a single Instruction object.

The functionality of this Quantum Environments can also be used with the gate_wrap decorator.

After compiling, the wrapped instruction can be retrieved using the .instruction attribute.

Parameters:
namestring, optional

The name of the resulting gate. The default is None.

Examples

We create some 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 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