InversionEnvironment#

class InversionEnvironment[source]#

This QuantumEnvironment can be used to invert (i.e. “dagger”) a block of operations.

An alias for this is invert.

Examples

We increment a QuantumFloat and afterwards revert using the InversionEnvironment:

from qrisp import QuantumFloat, invert

qf = QuantumFloat(4)

qf += 3

with invert():
    qf += 3
>>> print(qf)
{0: 1.0}
>>> print(qf.qs)
QuantumCircuit:
--------------
      ┌───────────┐┌──────────────┐
qf.0: ┤0          ├┤0             ├
      │           ││              │
qf.1: ┤1          ├┤1             ├
      │  __iadd__ ││  __iadd___dg │
qf.2: ┤2          ├┤2             ├
      │           ││              │
qf.3: ┤3          ├┤3             ├
      └───────────┘└──────────────┘
Live QuantumVariables:
---------------------
QuantumFloat qf

In the next example, we create a QuantumFloat and bring it into uniform superposition. We calculate the square and set a QuantumBool to True, based on if the result is less than 10. Finally, we use the InversionEnvironment to uncompute the result of the multiplication.

from qrisp import QuantumBool, h, q_mult, multi_measurement

qf = QuantumFloat(3)

h(qf)

mult_res = q_mult(qf, qf)

q_bool = QuantumBool()

with mult_res < 10:
    q_bool.flip()

with invert():
    q_mult(qf, qf, target = mult_res)

mult_res.delete(verify = True)
>>> print(multi_measurement([qf, q_bool]))
{(0, True): 0.125, (1, True): 0.125, (2, True): 0.125, (3, True): 0.125,
(4, False): 0.125, (5, False): 0.125, (6, False): 0.125, (7, False): 0.125}

Note

In many cases, this way of manually uncomputing only works if the uncomputed function (in this case q_mult) allows specifying the target variable. Using the redirect_qfunction decorator, you can turn any quantum function into it’s target specifiable version.