#
# Copyright 2020 Quantum Benchmark Inc.
#
"""
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 :py:class:`~trueq.results.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
#%%
# 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
#%%
# Increment the "111" result and print the updated results.
circuit.results["111"] += 5
circuit.results
#%%
# Add multiple results and print.
circuit.results += {"010": 1, "110": 10}
circuit.results
#%%
# Bitstrings can be replaced by their decimal equivalent.
circuit.results += {3: 1, 6: 10}
circuit.results
#%%
# 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])