PQC

Get introduced to the concept of PQC and implement the basic pqc-function.

Press + to interact
from functools import reduce
from qiskit import QuantumCircuit, Aer, execute, ClassicalRegister, QuantumRegister
from math import asin, sqrt, ceil
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
def prob_to_angle(prob):
"""
Converts a given P(psi) value into an equivalent theta value.
"""
return 2*asin(sqrt(prob))
def pqc(backend, prior, modifiers, shots=1, hist=False, measure=False):
# Prepare the circuit with qubits and a classical bit to hold the measurement
qr = QuantumRegister(7)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr) if measure else QuantumCircuit(qr)
# INSERT QUANTUM CIRCUIT HERE
qc = QuantumCircuit(4)
# measure the qubit only if we want the measurement to be included
if measure:
qc.measure(qr[0], cr[0])
results = execute(qc,backend, shots=shots).result().get_counts()
return plot_histogram(results, figsize=(12,4)) if hist else results
#CAPTION The basic pqc-function
qc=QuantumCircuit(7)

The shots parameter allows the caller to specify the number of times to run the quantum circuit. Note, this only works in combination with the qasm_simulator as the backend.

The hist parameter lets us specify whether we want to return a histogram (hist=True) or the raw data (hist=False).

The measure parameter allows us to easily switch between including the measurement of a qubit (measure=True) into the circuit or not. It’s usually helpful not to include the measurement during development because it allows us to use the statevector_simulator backend. This computes all the states of the individual qubits, butonce we have too many qubits in our circuit, things become hard to follow. It may be helpful to use the qasm_simulator backend and measure a single qubit that we’re interested in. We can use the shots parameter to run the circuit multiple times since we would only get a single number and not a probability distribution. This way, we get the approximate probability back. Of course, it’s only approximate because it’s an empiric reconstruction of the probability and not a precise calculation. Most times, however, it’s very accurate.

In this code listing, we’ve also added the prob_to_angle function we already used before, and we’ll use it here, too.

In the pqc function, we start with the definition of the QuantumCircuit. We’ll use seven qubits in total in this circuit in line 15. Depending on the measure parameter, we add a ClassicalRegister in line 16 to receive the measurement of a qubit in lines 22 to 24.

Once the quantum circuit is wholly specified, with or without a measurement, we execute it with the given backend and specify how many times (shots) we want the circuit to run in line 25.

Depending on the hist parameter, the function returns the plot of a histogram or the raw results in line 26.

Unless indicated otherwise, the following code listings become part of the pqc function. To keep them small, we skip the repetition of the code that we’ve already discussed.

Whenever we apply transformation gates in Qiskit, we need to specify the qubit index we want to apply the gate on. For once, numbers are not that easy to remember as words. Second, we might want to change the position a qubit with a specific purpose has. Therefore, we define ...