#initialization
import matplotlib.pyplot as plt
import numpy as np
import math
from qiskit.visualization import plot_distribution
# importing Qiskit
from qiskit import transpile, assemble
from qiskit_aer import Aer
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
# import basic plot tools and circuits
from qiskit.visualization import plot_histogram
from qiskit.circuit.library import QFT
from qiskit.visualization import plot_bloch_multivector
qpe = QuantumCircuit(3, 2)
qpe.x(2)
qpe.draw()
q_0: ───── q_1: ───── ┌───┐ q_2: ┤ X ├ └───┘ c: 2/═════
for qubit in range(2):
qpe.h(qubit)
qpe.draw()
┌───┐ q_0: ┤ H ├ ├───┤ q_1: ┤ H ├ ├───┤ q_2: ┤ X ├ └───┘ c: 2/═════
repetitions = 1
angle = 4*math.pi/3
for counting_qubit in range(2):
for i in range(repetitions):
qpe.cp(angle, counting_qubit, 2); # controlled-T
repetitions *= 2
qpe.draw()
┌───┐ q_0: ┤ H ├─■──────────────────────────── ├───┤ │ q_1: ┤ H ├─┼─────────■─────────■──────── ├───┤ │P(4π/3) │P(4π/3) │P(4π/3) q_2: ┤ X ├─■─────────■─────────■──────── └───┘ c: 2/═══════════════════════════════════
qpe.barrier()
# Apply inverse QFT
qpe = qpe.compose(QFT(2, inverse=True), [0,1])
# Measure
qpe.barrier()
for n in range(2):
qpe.measure(n,n)
qpe.draw()
┌───┐ ░ ┌──────────┐ ░ ┌─┐ q_0: ┤ H ├─■─────────────────────────────░─┤0 ├─░─┤M├─── ├───┤ │ ░ │ IQFT_dg │ ░ └╥┘┌─┐ q_1: ┤ H ├─┼─────────■─────────■─────────░─┤1 ├─░──╫─┤M├ ├───┤ │P(4π/3) │P(4π/3) │P(4π/3) ░ └──────────┘ ░ ║ └╥┘ q_2: ┤ X ├─■─────────■─────────■─────────░──────────────░──╫──╫─ └───┘ ░ ░ ║ ║ c: 2/══════════════════════════════════════════════════════╩══╩═ 0 1
aer_sim = Aer.get_backend('aer_simulator')
shots = 2048
t_qpe = transpile(qpe, aer_sim)
results = aer_sim.run(t_qpe, shots=shots).result()
answer = results.get_counts()
plot_distribution(answer)