QuantumString#

class QuantumString(size=0, qs=None, nisq_char=True)[source]#

The QuantumString is the quantum equivalent of a string. It is implemented as a QuantumArray of QuantumChars.

>>> from qrisp import QuantumString
>>> q_str = QuantumString(size = len("hello world"))
>>> q_str[:] = "hello world"
>>> print(q_str)
{'hello world': 1.0}

It is also possible to have a QuantumString containing non-nisq chars

>>> q_str_nn = QuantumString(size = len("hello world"), nisq_char = False)
>>> q_str_nn[:] = "hello world"
>>> print(q_str_nn)
{'hello world': 1.0}

This requires however considerably more qubits

>>> print(len(q_str.qs.qubits))
55
>>> print(len(q_str_nn.qs.qubits))
88

Similar to its parent class, the size of a QuantumString does not have to be specified at creation

>>> q_str = QuantumString()
>>> q_str[:] = "hello world"
>>> print(q_str)
{'hello world': 1.0}

Concatenation

QuantumStrings provide a number of methods to concatenate:

>>> q_str_0 = QuantumString()
>>> q_str_1 = QuantumString()
>>> q_str_2 = QuantumString()
>>> q_str_0[:] = "hello"
>>> q_str_1 += " "
>>> q_str_2[:] = "world"
>>> q_str_3 = q_str_1 + q_str_2
>>> q_str_0 += q_str_3
>>> print(q_str_0)
{'hello world': 1.0}

Note that these QuantumStrings share memory - i.e. if we modify a QuantumChar in one of them, this will potentially affect the others:

>>> from qrisp import h
>>> h(q_str_2[0][0])
>>> print(q_str_0)
{'hello world': 0.5, 'hello xorld': 0.5}