A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://qrisp.eu/reference/Algorithms/../Core/QuantumVariable.html below:

QuantumVariable — documentation

QuantumVariable#
class QuantumVariable(size, qs=None, name=None)[source]#

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}
Methods# De/Encoding states# Extending/Reducing the qubit count# Miscellaneous#

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