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

>>> example_qv.size
3

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
>>> print(s.qs)
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#

QuantumVariable.__init__(size[, qs, name])

Constructs a QuantumVariable - possibly with a given name or in a given QuantumSession.

QuantumVariable.delete([verify, recompute])

This method is for deleting a QuantumVariable and thus freeing up and resetting the used qubits.

QuantumVariable.get_measurement([plot, ...])

Method for quick access to the measurement results of the state of the variable.

QuantumVariable.most_likely(**kwargs)

Performs a measurement and returns the most likely outcome.

QuantumVariable.duplicate([name, qs, init])

Duplicates the QuantumVariable in the sense that a new QuantumVariable is created with same type and parameters but initialized in the \(\ket{0}\) state.

QuantumVariable.uncompute([do_it, recompute])

Method for automatic uncomputation.

De/Encoding states#

QuantumVariable.decoder(i)

The decoder method specifies how a QuantumVariable turns the outcomes of measurements into human-readable values.

QuantumVariable.encoder(value)

The encoder reverses the decoder, it turns human-readable values into integers.

QuantumVariable.encode(value[, permit_dirtyness])

The encode method allows to quickly bring a QuantumVariable in a desired computational basis state.

QuantumVariable.init_state(state_dic)

The init_state method allows the initialization of arbitrary quantum states.

QuantumVariable.init_from(other)

Method to initiate a QuantumVariable based on the state of another.

Extending/Reducing the qubit count#

QuantumVariable.extend(amount[, position])

This method is used to add more qubits to the QuantumVariable.

QuantumVariable.reduce(qubits[, verify])

Reduces the qubit count of the QuantumVariable by removing a specified set of qubits.

Miscellaneous#

QuantumVariable.app_phase_function(phi)

Applies a previously specified phase function to each computational basis state of the QuantumVariable using Gray-Synthesis.

QuantumVariable.custom(label_list[, ...])

Creates a QuantumVariable with customized outcome labels.