The QuantumVariable is the quantum equivalent of a regular variable in classical programming languages. All quantum types inherit from this class. The QuantumVariable allows many automizations and quality of life improvements such as hidden qubit management, de/encoding to human readable labels or typing.
Each QuantumVariable is registered in a QuantumSession. It can be accessed using the .qs
attribute:
>>> from qrisp import QuantumVariable >>> example_qv = QuantumVariable(3) >>> quantum_session = example_qv.qs
The qubits of the QuantumVariable are stored as a list in the .reg
attribute
>>> qubits = example_qv.reg
To quickly access the qubits of a given variable, we use the [ ] operator:
>>> qubit_2 = example_qv[2]
We can find out about the amount of qubits in the QuantumVariable with the .size
attribute
Naming
QuantumVariables can be given names to identify them independently of their naming as Python objects.
>>> example_qv_2 = QuantumVariable(3, name = "alice") >>> example_qv_2.name 'alice'
If not explicitely specified during construction, a name is determined automatically. Qrisp will try to infer the name of the Python variable and if that fails, a generic name is given.
>>> example_qv.name 'example_qv'
In order to keep the generated quantum circuits comprehensive, the qubits are named after their containing QuantumVariable with an extra number, which indicates their index.
>>> from qrisp import cx >>> cx(example_qv, example_qv_2) >>> print(example_qv.qs)
QuantumCircuit: -------------- example_qv.0: âââ ââââââââââââ â example_qv.1: âââ¼âââââ âââââââ â â example_qv.2: âââ¼âââââ¼âââââ ââ âââ´ââ â â alice.0: ⤠X ââââ¼âââââ¼ââ ââââââââ´ââ â alice.1: âââââ⤠X ââââ¼ââ ââââââââ´ââ alice.2: ââââââââââ⤠X â âââââ Live QuantumVariables: --------------------- QuantumVariable example_qv QuantumVariable alice
QuantumSessions can only contain uniquely named QuantumVariables. If two QuantumSessions are merged containing identically named QuantumVariables, the more recently created QuantumVariable will be renamed:
from qrisp import QuantumFloat s = QuantumFloat(5) for i in range(4): temp = QuantumFloat(4) temp[:] = 2**i s += temp
QuantumCircuit: -------------- ââââââââââââââââââââââââââââââââââââââââââââââââââââ s.0: ââââââ¤0 ââ¤0 ââ¤0 ââ¤0 â â ââ ââ ââ â s.1: ââââââ¤1 ââ¤1 ââ¤1 ââ¤1 â â ââ ââ ââ â s.2: ââââââ¤2 ââ¤2 ââ¤2 ââ¤2 â â ââ ââ ââ â s.3: ââââââ¤3 ââ¤3 ââ¤3 ââ¤3 â â ââ ââ ââ â s.4: ââââââ¤4 __iadd__ ââ¤4 ââ¤4 ââ¤4 â ââââââ ââ ââ ââ â temp.0: ⤠X ââ¤5 â⤠â⤠â⤠â ââââââ ââ ââ ââ â temp.1: ââââââ¤6 â⤠__iadd__ â⤠â⤠â â ââ ââ ââ â temp.2: ââââââ¤7 â⤠â⤠â⤠â â ââ ââ ââ â temp.3: ââââââ¤8 â⤠â⤠__iadd__ â⤠â ââââââââââââââ ââ ââ â temp_1.0: âââââââââââââââââââ¤5 â⤠â⤠â âââââ â ââ ââ â temp_1.1: ⤠X âââââââââââââââ¤6 â⤠â⤠__iadd__ â âââââ â ââ ââ â temp_1.2: âââââââââââââââââââ¤7 â⤠â⤠â â ââ ââ â temp_1.3: âââââââââââââââââââ¤8 â⤠â⤠â ââââââââââââââ ââ â temp_2.0: ââââââââââââââââââââââââââââââââ¤5 â⤠â â ââ â temp_2.1: ââââââââââââââââââââââââââââââââ¤6 â⤠â âââââ â ââ â temp_2.2: ⤠X ââââââââââââââââââââââââââââ¤7 â⤠â âââââ â ââ â temp_2.3: ââââââââââââââââââââââââââââââââ¤8 â⤠â ââââââââââââââ â temp_3.0: âââââââââââââââââââââââââââââââââââââââââââââ¤5 â â â temp_3.1: âââââââââââââââââââââââââââââââââââââââââââââ¤6 â â â temp_3.2: âââââââââââââââââââââââââââââââââââââââââââââ¤7 â âââââ â â temp_3.3: ⤠X âââââââââââââââââââââââââââââââââââââââââ¤8 â âââââ âââââââââââââ Live QuantumVariables: --------------------- QuantumFloat s QuantumFloat temp QuantumFloat temp_1 QuantumFloat temp_2 QuantumFloat temp_3
Renaming does not happen for names given through the name
keyword, unless the name ends with a *
.
>>> example_qv_3 = QuantumVariable(3, name = "alice") >>> cx(example_qv, example_qv_3) Exception: Tried to merge QuantumSession containing identically named QuantumVariables >>> example_qv_4 = QuantumVariable(3, name = "alice*") >>> cx(example_qv, example_qv_4) >>> example_qv_4.name 'alice_1'
Examples
Writing a function that brings an arbitrary QuantumVariable into a GHZ state
from qrisp import QuantumVariable, h, cx def GHZ(qv): h(qv[0]) for i in range(1, qv.size): cx(qv[0], qv[i])
Evaluation:
>>> qv = QuantumVariable(5) >>> GHZ(qv) >>> print(qv) {'00000': 0.5, '11111': 0.5}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4