Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
Quantum volume is a measurement of the errors characterizing a chosen quantum hardware.The quantum volume is a result of running a circuit based on principles of randomness and statistical analysis, which provides a single number to compare different hardware backends.The scheme of the quantum volume [1]:
For a number of qubits n, a circuit is made of the n quantum layer.
Each layer consists of a unitary operation between pairs of n qubits.
The pairs are chosen at random. If n is odd, one of them does not have an operation.
The unitary operation between each pair is the Haar random matrix; i.e., an SU(4) operation containing a random complex number in such a manner that the probability of measuring a quantum state is kept with uniform distribution.
A single circuit of n qubits is measured and the heavy output probability (i.e., the probability of measuring the states above the median value) is calculated.
Due to the nature of the distribution of random complex numbers, one can evaluate that for an ideal case (no noises), the heavy output probability should be ~8
For an assessment of the quantum volume, the demand subsides to the following inequality: (1) Pheavy_outputs≤2/3.
For a given output, to get the quantum volume, repeat Items 1-4 for an increasing number of qubits until the inequality described in Item 4 does not hold. To ensure it, the circuits are created many times and the average and standard deviation are taken into account.
The quantum volume is two to the power of the number of qubits, such that they pass inequality (1) as per the procedure described in Items 1-
The heavy output probability is a good measurement of the quality of the circuit, as noise reduces the probabilities of uniform distribution.
While this is so, consider that there are many components to the results of the procedure—not only the hardware noises, but also the connectivity map, the quality of the transpilation, and even the quantum software that translates the circuit into basis gates for the hardware, thus contributing to the circuit depth.This demonstration shows the code for implementing the steps to calculate the quantum volume using the Classiq platform and an example of such calculations for several quantum simulators and hardware backends.
Create a function, generating a (n,n) sized Haar random unitary matrix [2].This matrix contains a random complex number that is distributed evenly in the 2n space of quantum states.The Haar distribution indicates how to weight the elements of U(N) such that uniform distribution occurs in the parameter space.
import numpy as npfrom numpy.linalg import qrfrom scipy.stats import unitary_groupdef haar(n): u1 = unitary_group.rvs(n) u2 = unitary_group.rvs(n) Z = u1 + 1j * u2 Q, R = qr(Z) Lambda = np.diag([R[i, i] / np.abs(R[i, i]) for i in range(n)]) return np.dot(Q, Lambda)
The qv_model function creates the quantum volume model for a given N number of qubits.For N qubits, the circuit must include N quantum volume layers.The layers are built using the qv_layer function, which creates random pairing between the N qubits. (For an odd number, a randomly chosen qubit is not operational.) Between each pair, a unitary gate operates, consisting of a Haar random unitary matrix of size
import mathimport randomfrom classiq import *@qfuncdef qv_layer(N: int, target: QArray): # Step 1: start with a shuffle of the qubits qubit_list = list(range(N)) random.shuffle(qubit_list) for idx in range(math.floor(N / 2)): # Step 2: Isolate the qubit pairs for the layers a = qubit_list[idx] b = qubit_list[math.floor(N / 2) + idx] # Step 3: Generate the random matrix (this needs to change for the random matrix when possible) gate_matrix = haar(4).tolist() unitary(gate_matrix, [target[a], target[b]])def qv_model(N): @qfunc def main(target: Output[QArray]): allocate(N, target) repeat(N, lambda _: qv_layer(N, target)) return main
The execution and analysis part consists of these functions:
execute_qv sends a quantum program for execution on a given quantum hardware with a specified number of shots.
The function returns the results of the execution from the hardware.
heavy_outputs_prob analyzes the results from execution and returns the heavy output probability; i.e., the probability for a single state in the space to be greater than the median value (median = “middle” of a sorted list of numbers).
The round_significant function rounds a number for one significant figure.
def execute_qv(qprog, num_shots, preferences): execution_prefs = ExecutionPreferences( num_shots=num_shots, backend_preferences=preferences ) with ExecutionSession(qprog, execution_prefs) as es: res = es.sample() return res
def heavy_outputs_prob(results): d = list(results.counts.values()) med = np.median(d) heavy_outputs_prob = 0 # print(med) for count, item in enumerate(d): if item >= med: heavy_outputs_prob = heavy_outputs_prob + item return heavy_outputs_prob
from math import floor, log10def round_significant(x): return round(x, -int(floor(log10(abs(x)))))
Using the previously defined functions, find_qv finds the quantum volume value for defined parameters including hardware definitions.The find_qv function sends the value of heavy output probability for each number of qubits defined (between min_qubit and max_qubits).This repeats num_trials times. Then, the heavy output probability is averaged, and the standard deviation is calculated. If the number of qubits chosen for the circuit is less than the number of qubits in the chosen hardware, the qubits are randomly picked to run according to the rules of the hardware provider.The quantum volume qubits number is defined as the larger number of qubits for which the heavy output probability, decreased by two sigma (twice the standard deviation), is greater than or equal to 2/
The quantum volume is two to the power of the number of quantum volume qubits.
Note that if the result given for the log2 of the quantum volume is the same as the chosen max_qubits, there is a possibility that the quantum volume is greater than found by the function. In this case, run the program for a greater span.
from tqdm import tqdm# For testing, we save all qprogs generated in the notebookqprogs = []def find_qv(num_trials, num_shots, min_qubits, max_qubits, preferences):### initialization qubit_num = range(min_qubits, max_qubits + 1) heavy_list = np.zeros(max_qubits - min_qubits + 1) std_list = np.zeros(max_qubits - min_qubits + 1) qubit_v = 0### calculate the heavy outputs for each number of qubits for num in tqdm(qubit_num): heavy_outputs = 0 std = 0 heavytostd = np.zeros(num_trials) for idx in tqdm(range(num_trials)): qprog = synthesize(qv_model(num)) qprogs.append(qprog) results = execute_qv(qprog, num_shots, preferences) heavy_temp = heavy_outputs_prob(results) heavy_outputs = heavy_outputs + heavy_temp heavytostd[idx] = heavy_temp s = num - min_qubits heavy_list[s] = heavy_outputs / (num_trials * num_shots) temp_hl = heavy_outputs / (num_trials * num_shots) std = np.std(heavytostd) / (num_trials * num_shots) std_list[s] = std temp_std = round_significant(std) print( f"for {num} qubits the heavy outputs probability is: {temp_hl} with {temp_std} standard deviation" )### determine the quantum volume for num in qubit_num: s = num - min_qubits heavy_is = heavy_list[s] - 2 * (std_list[s]) if heavy_is >= 2 / 3: qubit_v = num else: break qv = 2**qubit_v print(f"##### The quantum volume is {qv} #####") return qv
num_trials = 10 # number of times to run the QV circuit for each number of qubits. Best: 200 or morenum_shots = 100 # number of runs for each execution. Best: 1000 or morepreferences = ClassiqBackendPreferences( backend_name=ClassiqSimulatorBackendNames.SIMULATOR)min_qubits = 3max_qubits = 6qv = find_qv(num_trials, num_shots, min_qubits, max_qubits, preferences)
num_trials = 10 # number of times to run the QV circuit for each number of qubitsnum_shots = 3 # number of runs for each executionpreferences = AzureBackendPreferences(backend_name="Rigetti.Qpu.Aspen-M-3")min_qubits = 2max_qubits = 3# qv = find_qv_trials, num_(numshots, min_qubits, max_qubits, preferences)
preferences = IBMBackendPreferences( backend_name="ibm_fez", access_token="my-access-token", channel="ibm_cloud", instance_crn="instance-CRN",)num_trials = 5 # number of times to run the QV circuit for each number of qubitsnum_shots = 10 # number of runs for each executionmin_qubits = 2max_qubits = 4# qv = find_qv(num_trials, num_shots, min_qubits, max_qubits, preferences)
preferences = IBMBackendPreferences( backend_name="ibm_marrakesh", access_token="my-access-token", channel="ibm_cloud", instance_crn="instance-CRN",)num_trials = 1 # number of times to run the QV circuit for each number of qubitsnum_shots = 10 # number of runs for each executionmin_qubits = 2max_qubits = 3# qv = find_qv(num_trials, num_shots, min_qubits, max_qubits, preferences)
preferences = IBMBackendPreferences( backend_name="ibm_sherbrooke", access_token="my-access-token", channel="ibm_cloud", instance_crn="instance-CRN",)num_trials = 1 # number of times to run the QV circuit for each number of qubitsnum_shots = 10 # number of runs for each executionmin_qubits = 2max_qubits = 3# qv = find_qv(num_trials, num_shots, min_qubits, max_qubits, preferences)