[1]:
import quanguru as qg
8 - qRes attribute: an intro to Results objects#
This tutorial introduces the qRes
attribute of QuantumSystem
objects. qRes
itself is also an object, but, for almost all cases, we do not need to create it explicitly. All the relevant objects (like QuantumSystem
) already come with a qRes
attribute, and the purpose of this tutorial is to explain its basics.
Let’s first create a QuantumSystem
[2]:
qub = qg.QuantumSystem()
The qRes
attribute is an instance of qResults
class.
[3]:
print(qub.qRes.__class__)
<class 'quanguru.classes.QRes.qResults'>
qResults
objects have two main dictionaries (specifically defaultdict
) to store states
and results
. Here, the states
means the state of the quantum system as a function of time, and the results
means any other quantity (like expectation values) that we compute as a function of time.
The library takes care of storing the states, and the users only need to store the results of their computation, which will be covered in later tutorials. In this tutorial, we explain how to reach these attributes and store some quantity in them.
We can reach states
and results
through the qRes
attribute as below, which are just empty defaultdict
.
[4]:
print(qub.qRes.states, qub.qRes.resultsDict)
defaultdict(<class 'list'>, {}) defaultdict(<class 'list'>, {})
and, for internally created qRes
, we can do the same thing also through the QuantumSystem
(by omitting the .qRes
)
[5]:
print(qub.qRes.states is qub.states, qub.qRes.resultsDict is qub.resultsDict)
True True
As explained above, the library internally manages the state storage, so we do not need to worry about how to store the states. We will cover more examples for states
in later tutorials and focus on storing results
in here.
Since these attributes are simply defaultdict
that create an empty list
as default for a given key, we can use any key to store a list of values as below
[6]:
# store some values
for i in range(10):
qub.resultsDict["1to10"].append(i+1)
# print those values
print(qub.resultsDict["1to10"])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
We will cover when and how to use above, but, for now, the key point here is that you can simply call append with an arbitrary key, because .resultsDict
returns a defaultdict
.
qResults
objects actually uses setter/getter
and provide an alternative notation for the same task as below. Notice that, in this case, we use .result
(not results
) as it is a different setter. As noted above, we will cover where and how these will be used, and don’t worry if you don’t understand the implementation details.
[7]:
# store some values
for i in range(10):
qub.qRes.singleResult = ["11to20", i+11]
# print those values
print(qub.resultsDict["11to20"])
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]