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(params[,Β method])

Initialize an arbitrary quantum state on this quantum variable.

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.