# Recording Results¶

import trueq as tq
import matplotlib.pyplot as plt
import itertools


To begin, we initialize a 3-qubit circuit. It is important that we add three measurement operations to the circuit because the total number of measurement operations dictates the length of bitstrings that are allowed by the circuit.

circuit = tq.Circuit({(0, 1): tq.Gate.cz, (2,): tq.Gate.x})
circuit.measure_all()


We can manually set the value of results with anything dict-like. This is cast to a Results object when stored in the circuit.

The order of the bits in the bitstring, from left to right, corresponds to the order of the measurement operations in the circuit with respect to sorted qubit labels. Thus the left-most bit in “011” corresponds to the measurement of qubit 0 in this example.

circuit.results = {"011": 5, "101": 15}
circuit.results


Out:

Results({'011': 5, '101': 15})


The results object has many convenience methods. For example, we can add the “111” result and print the updated results.

circuit.results["111"] = 10
circuit.results


Out:

Results({'011': 5, '101': 15, '111': 10})


Increment the “111” result and print the updated results.

circuit.results["111"] += 5
circuit.results


Out:

Results({'011': 5, '101': 15, '111': 15})


circuit.results += {"010": 1, "110": 10}
circuit.results


Out:

Results({'011': 5, '101': 15, '111': 15, '010': 1, '110': 10})


Bitstrings can be replaced by their decimal equivalent.

circuit.results += {3: 1, 6: 10}
circuit.results


Out:

Results({'011': 6, '101': 15, '111': 15, '010': 1, '110': 20})


Here, we display the results entered so far as a bar plot.

labels = ["".join(s) for s in itertools.product("01", repeat=circuit.n_sys)]
plt.bar(labels, [circuit.results.get(s) for s in labels]) Out:

<BarContainer object of 8 artists>


Total running time of the script: ( 0 minutes 0.075 seconds)

Gallery generated by Sphinx-Gallery