<jupyter_start><jupyter_text>Quantum VolumeHere we show off using the expectation value functionality of the M3 distribution classes using Quantum Volume (QV) as an example.  Here we formulate QV as an expectation value of a projector onto the heavy-output elements on a distribution.<jupyter_code>import numpy as np
from qiskit import *
from qiskit.quantum_info import Statevector
from qiskit.circuit.library import QuantumVolume
import mthree
from qiskit.test.mock import FakeBurlington
noisy_sim = FakeBurlington()<jupyter_output><empty_output><jupyter_text>QV is defined in terms of heavy-ouputs of a distribution.  Heavy-outputs are those bit-strings that are those that have probabilities above the median value of the distribution.  Below we define the projection operator onto the set of bit-strings that are heavy-outputs for a given distribution.<jupyter_code>def heavy_projector(qv_probs):
    """Forms the projection operator onto the heavy-outputs of a given probability distribution.
    
    Parameters:
        qv_probs (dict): A dictionary of bitstrings and associated probabilities.
        
    Returns:
        dict: Projector onto the heavy-set.
    """
    median_prob = np.median(list(qv_probs.values()))
    heavy_strs = {}
    for key, val in qv_probs.items():
        if val > median_prob:
            heavy_strs[key] = 1
    return heavy_strs<jupyter_output><empty_output><jupyter_text>Now we generate 10 QV circuits as our dataset.<jupyter_code># Generate QV circuits
N = 10
qv_circs = [QuantumVolume(5) for _ in range(N)]<jupyter_output><empty_output><jupyter_text>Next, we have to determine the heavy-set of each circuit from the ideal answer, and then pass this along to our heavy-set projector function that we defined above.<jupyter_code># Compute ideal distributions and projectors on the heavy set.
ideal_probs = [Statevector.from_instruction(circ).probabilities_dict() for circ in qv_circs]
heavy_projectors = [heavy_projector(probs) for probs in ideal_probs]<jupyter_output><empty_output><jupyter_text>Now, in preparation for actual execution, we add measurements to the end of our QV circuits, and compile them for the target device<jupyter_code># Add meauserements to circuits and transpile
trans_circs = transpile([circ.measure_all(inplace=False) for circ in qv_circs], noisy_sim,
                        layout_method='sabre', routing_method='sabre', optimization_level=3)<jupyter_output><empty_output><jupyter_text>Because the SWAP mapping of the circuit permutes the qubit states, we need the final measurement mapping for each circuit to know which physical qubit corresponds to each measured bit:<jupyter_code># Determine final qubit mappings
maps = mthree.utils.final_measurement_mapping(trans_circs)<jupyter_output><empty_output><jupyter_text>We now calibrate our M3 mitigator over only those qubits used in the QV circuits. (We use the `independent` method since it is much faster to do so on a simulator.)  We can directly pass the maps to the calibrtion routine and it will calibrate over the ful lset of qubits used in the mappings.<jupyter_code>mit = mthree.M3Mitigation(noisy_sim)
mit.cals_from_system(maps, method='independent')<jupyter_output><empty_output><jupyter_text>We are now ready to execute the circuits on the target backend, and mitigate the resulting raw counts.  Here we directly pass the maps which will internally be converted into the correct qubit lists:<jupyter_code># Execute circuits and mitigate
raw_counts = noisy_sim.run(trans_circs, shots=8192).result().get_counts()
quasi_collection = mit.apply_correction(raw_counts, maps)<jupyter_output><empty_output><jupyter_text>The value needed to determine if each circuit represents a passing QV value is determined by the expectation value of the heavy projector for each circuit.  First let us evaluate the raw counts using the M3 utils function `expval`:<jupyter_code>raw_expvals = mthree.utils.expval(raw_counts, heavy_projectors)
raw_expvals<jupyter_output><empty_output><jupyter_text>A passing QV score is one where the expectation value is above 2/3:<jupyter_code>raw_expvals > 2/3<jupyter_output><empty_output><jupyter_text>Now let us do the same analysis for the mitigated data:<jupyter_code># Determine expectation value of heavy set prjector
mit_expvals = quasi_collection.expval(heavy_projectors)
mit_expvals
# Check if the scores are passing or not
mit_expvals > 2/3<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06299390381575977, 'fraction_numerical': 0.004741476631293746, 'mean_word_length': 5.722306525037936, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '437012', 'license': 'Apache-2.0', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1275, 'n_words': 473, 'path': 'tutorials/03_qv_example.ipynb', 'repo_name': 'gadial/mthree', 'size': '4429'}