- class QuantumSession(backend=None)#
>>> 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, qv)
Nevertheless, users are encouraged to use the designated gate application function in order to reduce code cluttering.
>>> from qrisp import cx >>> cx(qv, qv)
QuantumSessions can be visualized by calling
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, qv_b) >>> qv_a.qs == qv_b.qs True
QuantumSessions can be given a default backend on which to evaluate circuits:
>>> from qrisp.interface import VirtualQiskitBackend >>> qiskit_backend = instantiate_qiskit_backend() >>> qs = QuantumSession(backend = VirtualQiskitBackend(qiskit_backend))
In this piece of code, we assume that the function
instantiate_qiskit_backendcreates a Qiskit backend instance (which could either be the QASM Simulator or a real backend). We then hand this to the VirtualQiskitBackend 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.pywill be used.
Note that it is not possible to merge two QuantumSessions with differing, non-trivial backends.
Constructs a QuantumSession
Method to compile the QuantumSession into a QuantumCircuit.
Returns a representation of the statevector.
Method to calculate the circuit depth of this QuantumSession
Method to determine the amount of CNOT gates used in this QuantumSession.