diff --git a/exercises/P2e_QPE.py b/exercises/P2e_QPE.py new file mode 100644 index 0000000000000000000000000000000000000000..9317f6c85a8c023fbf246a36a77e20e518554cad --- /dev/null +++ b/exercises/P2e_QPE.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +''' +Problem 2e of exercise 11: Quantum phase estimation of a T gate. +''' + +#initialization +import matplotlib.pyplot as plt +import numpy as np +import math + +# importing Qiskit +from qiskit import Aer, transpile, assemble +from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister + +# import basic plot tools +from qiskit.visualization import plot_histogram + +# you may use this function to execute the inverse fourier transform or write your own +def qft_dagger(qc, n): + ''' + Perform inverse quantum fourier transform on the first n qubits of + the quantum circuit qc. + ''' + for qubit in range(n//2): + qc.swap(qubit, n-qubit-1) + for j in range(n): + for m in range(j): + qc.cp(-math.pi/float(2**(j-m)), m, j) + qc.h(j) + + +def main(): + # (i) explain the following line: Why is the initialization done like this? Which qubits fulfill which function? + qpe = QuantumCircuit(QuantumRegister(4), ClassicalRegister(3)) + + # (ii) add the correct qubit initialization and control gates for quantum phase estimation + + # (iii) add inverse qft and counting qubit measurements + + # run your circuit with this simulator configuration + aer_sim = Aer.get_backend('aer_simulator') + shots = 2048 + t_qpe = transpile(qpe, aer_sim) + qobj = assemble(t_qpe, shots=shots) + results = aer_sim.run(qobj).result() + answer = results.get_counts() + + plot_histogram(answer) + # (iv) what is the result and the resulting phase? what is the error? + + +if __name__ == '__main__': + main() diff --git a/exercises/P2e_QPE_T-Gate.ipynb b/exercises/P2e_QPE_T-Gate.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..a120b496189f17998873d8f21f1792eba941b992 --- /dev/null +++ b/exercises/P2e_QPE_T-Gate.ipynb @@ -0,0 +1,112 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "c0d4fe7e", + "metadata": {}, + "outputs": [], + "source": [ + "#initialization\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import math\n", + "\n", + "# importing Qiskit\n", + "from qiskit import IBMQ, Aer, transpile, assemble\n", + "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister\n", + "\n", + "# import basic plot tools\n", + "from qiskit.visualization import plot_histogram" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b32eefed", + "metadata": {}, + "outputs": [], + "source": [ + "# (i) explain the following line: Why is the initialization done like this? Which qubits fulfill which function?\n", + "qpe = QuantumCircuit(QuantumRegister(4), ClassicalRegister(3))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "31cb2e3e", + "metadata": {}, + "outputs": [], + "source": [ + "# (ii) add the correct qubit initialization and control gates for quantum phase estimation \n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "06e68e51", + "metadata": {}, + "outputs": [], + "source": [ + "# you may use this function to execute the inverse fourier transform or write your own\n", + "def qft_dagger(qc, n):\n", + " for qubit in range(n//2):\n", + " qc.swap(qubit, n-qubit-1)\n", + " for j in range(n):\n", + " for m in range(j):\n", + " qc.cp(-math.pi/float(2**(j-m)), m, j)\n", + " qc.h(j)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5eeac6dc", + "metadata": {}, + "outputs": [], + "source": [ + "# (iii) add inverse qft and counting qubit measurements\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac7b2c7f", + "metadata": {}, + "outputs": [], + "source": [ + "# run your circuit with this simulator configuration\n", + "aer_sim = Aer.get_backend('aer_simulator')\n", + "shots = 2048\n", + "t_qpe = transpile(qpe, aer_sim)\n", + "qobj = assemble(t_qpe, shots=shots)\n", + "results = aer_sim.run(qobj).result()\n", + "answer = results.get_counts()\n", + "\n", + "plot_histogram(answer)\n", + "# (iv) what is the result and the resulting phase? what is the error? " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}