QuantumSession#
- class QuantumSession(backend=None)[source]#
The QuantumSession class manages the life cycle of QuantumVariables and enables features such as QuantumEnvironments or Uncomputation. To create a QuantumSession, we call the constructor
>>> from qrisp import QuantumSession >>> qs = QuantumSession()
To create a QuantumVariable within that QuantumSession, we hand it over to the QuantumVariable constructor:
>>> from qrisp import QuantumVariable >>> qv = QuantumVariable(3, qs = qs)
As an inheritor of the QuantumCircuit class, QuantumSession objects can also be used for circuit construction.
>>> qv.qs.cx(qv[0], qv[1])
Nevertheless, users are encouraged to use the designated gate application function in order to reduce code cluttering.
>>> from qrisp import cx >>> cx(qv[0], qv[1])
QuantumSessions can be visualized by calling
print
on them.>>> print(qv.qs)
QuantumCircuit: -------------- qv.0: ──■────■── ┌─┴─┐┌─┴─┐ qv.1: ┤ X ├┤ X ├ └───┘└───┘ qv.2: ────────── Live QuantumVariables: --------------------- QuantumVariable qv
If not specified, QuantumVariables will create their own QuantumSession and register themselves in it.
QuantumSessions can be manually merged using the merge function.
>>> from qrisp import merge >>> qs_2 = QuantumSession() >>> qs_2 == qs False >>> merge(qs, qs_2) >>> qs == qs_2 True
Note that merge also works for QuantumVariables, lists of QuantumSession and lists of QuantumVariables.
If an entangling operation between two QuantumVariables which are registered in different QuantumSessions is executed, these QuantumSessions are automatically merged. For more details on automatic QuantumSession merging check the session merging documentation.
>>> qv_a = QuantumVariable(2) >>> qv_b = QuantumVariable(2) >>> qv_a.qs == qv_b.qs False >>> from qrisp import cx >>> cx(qv_a[0], qv_b[0]) >>> qv_a.qs == qv_b.qs True
QuantumSessions can be given a default backend on which to evaluate circuits:
>>> from qrisp.interface import QiskitBackend >>> qiskit_backend = instantiate_qiskit_backend() >>> qs = QuantumSession(backend = QiskitBackend(qiskit_backend))
In this piece of code, we assume that the function
instantiate_qiskit_backend
creates a Qiskit backend instance (which could either be the QASM Simulator or a real backend). We then hand this to the QiskitBackend constructor to turn it into a Qrisp backend. Now, any measurements of variables that are registered in this session will be evaluated on that backend.If no backend is given, the backend specified in
default_backend.py
will be used.Note that it is not possible to merge two QuantumSessions with differing, non-trivial backends.
Methods#
|
Constructs a QuantumSession |
|
Method to compile the QuantumSession into a QuantumCircuit. |
|
Returns a representation of the statevector. |
|
Returns the depth of the QuantumCircuit. |
Method to determine the amount of CNOT gates used in this QuantumSession. |