[1]:
import quanguru as qg
0 - name and alias of QuanGuru objects#
Instances of QuanGuru
classes are created with unique default names. The name
of an object is assigned by the library and it cannot be changed later. In later tutorials, we will demonstrate various cases where the (unique) name
of an object enables (or used in the implementation of) various useful functionalities. In this tutorial, we explain the convention for these names.
In certain cases, we might find it useful/easier if could change the name
of an object to anything we want but still have the same useful functionalities. QuanGuru
objects come with an alias
attribute introduced for this purpose. alias
attribute stores (in a list) any number of user defined aliases to an object, which extends all the functionalities available to name
to aliases. Therefore, like the name
, aliases need to be unique to an object, meaning the same alias
cannot be assigned to different objects.
name attribute#
The name
of an object is created by using the name of its class and the number of instances. Let’s use the QuantumSystem
and Qubit
classes to demonstrate this.
[2]:
# create the very first instance of QuantumSystem
qs1 = qg.QuantumSystem()
# let's print its name, which should be QuantumSystem + 1 = QuantumSystem1
print(qs1.name)
# create the first Qubit instance
qub1 = qg.Qubit()
# let's print its name, which should be Qubit + 1 = Qubit1
print(qub1.name)
QuantumSystem1
Qubit1
[3]:
# create the second instance of QuantumSystem
qs2 = qg.QuantumSystem()
# let's print its name, which should be QuantumSystem + 2 = QuantumSystem2
print(qs2.name)
# create the second Qubit instance
qub2 = qg.Qubit()
# let's print its name, which should be Qubit + 2 = Qubit1
print(qub2.name)
QuantumSystem2
Qubit2
aliases for the objects#
Notice that if you run the above cell more than once, you create a new QuantumSystem
(and Qubit
) instance each time you run it, increasing the number of instance count. Therefore, in these sorts of situations, if we did not have alias
alternative, we would be required to keep good track of how many instances we created. Additionally, unlike the name
of an object that is created uniquely internally for each instance, alias
are defined by the user. This means that if we
are assigning an alias
to an object in a cell (as below), we would get an error when we try to run it more than once, because we would be trying to assign the alias
of an previous instance to a new instance.
[4]:
# create the third instance of QuantumSystem and also assign an alias
qs3 = qg.QuantumSystem(alias='test alias')
# let's print its name, which should be QuantumSystem + 3 = QuantumSystem3
print(qs3.name)
# let's print its alias, which will be list containing the alias we asssigned
print(qs3.alias)
QuantumSystem3
['test alias']
[5]:
# create the very first instance of QuantumSystem
try:
qsErrorForAlias = qg.QuantumSystem(alias='test alias')
except ValueError as ve:
print(ve)
Given alias (test alias) already exist and is assigned to: QuantumSystem3