# Recording Results

import trueq as tq


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()
circuit

True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
 Circuit Key: No key present in circuit. Marker 0 Compilation tools may only recompile cycles with equal markers. (0, 1): Gate.cz Name: Gate.cz Aliases: Gate.cz Likeness: CNOT Generators: 'ZZ': -90.0 'ZI': 90.0 'IZ': 90.0 Matrix: 1.00 1.00 1.00 -1.00 (2): Gate.x Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: 'X': 180.0 Matrix: 1.00 1.00 1 Marker 1 Compilation tools may only recompile cycles with equal markers. (0): Meas() Name: Meas() (1): Meas() Name: Meas() (2): Meas() Name: Meas()

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

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

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


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

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

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


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

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

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


Results can also be set by passing vectors of the correct size.

circuit.results.from_vec([0, 0, 0, 1, 0, 0, 10, 0])
circuit.results.vec

array([ 0,  0,  0,  1,  0,  0, 10,  0])


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

circuit.results.plot()


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

Gallery generated by Sphinx-Gallery