Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 5: Basic Quantum Programs\n", "\n", "First, we will begin by importing Qiskit, IBM's quantum computing software that is...

1 answer below »

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework 5: Basic Quantum Programs\n",
"\n",
"First, we will begin by importing Qiskit, IBM's quantum computing software that is written in Python! To create an experiment and run it, we will need to use Qiskit's Circuits, Registers, and Compilers. Programming using qiskit allows us to programmatically extend QASM code to use for loops and if statements to design circuits faster. QASM is an assembler-like instruction set that allows you to specify quantum circuits."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Import li
aries and simulator\n",
"Aes is a backend simulator for the IBM quantum computers (other backends are run on real QC). Numpy is good with a
ays and matrices. pyplot is for visualization of probability distributions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import qiskit\n",
"from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute, Aer\n",
"\n",
"\n",
"from qiskit.compiler import transpile\n",
"from qiskit.tools.visualization import plot_histogram\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"#settings and backends\n",
"shots = 1024\n",
"simulator = Aer.get_backend('qasm_simulator')\n",
"state_vector_sim = Aer.get_backend('statevector_simulator')\n",
"\n",
"print('qiskit vers.= %s'%qiskit.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Demo: Superposition\n",
"\n",
"First, we will demonstrate quantum superposition. This will be accomplished using a one-qubit quantum circuit that has a single gate: the Hadamard operation. The qubit is initialized to the computational basis vector $|0\\rangle$ and is then applied to the Hadamard gate. Whenever the resulting state vector for the evolved wavefunction is examined, it is observed that the probability amplitudes for both $|0\\rangle$ and $|1\\rangle$ are equal to $\\frac{1}{\\sqrt{2}}$, indicating maximal superposition of the qubit. A measurement operation placed after the Hadamard gate causes it to collapse into one of the comutational basis states. We will simulate this circuit 1,024 times (this is called the number of \"shots\" in the IBMQ environment). The measurement operator \"observes\" the quantum information and places the measurement output into the classical register. Once the circuit is complete with registers, a state transformation, and a measurement operation, experiments will be run using the Qiskit Aer simulator. Note how after each run, the output distribution varies slightly, but is close to a 50/50 split between the basis states $|0\\rangle$ and $|1\\rangle$ whenever the input quantum state is in a basis state."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Quantum registers are where the qubit values are initialized and stored.\n",
"\n",
"Classical registers are used to store the results of quantum registers when measured.`qc.measure(qr,qc)`\n",
"\n",
"Commands `qc.h` are the hadamard gate.\n",
"\n",
"NOTE: I could not get the \"plot_histogram\" function to work on my Mac OSX machine, so I inserted the two \"plt.bar\" and \"plt.show\" function calls. If you are using a Windows machine, you may want to uncomment the \"plot_histogram\" function call and comment out the \"plt.bar\" and \"plt.show\" functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qr = QuantumRegister(1, 'q_reg')\n",
"cr = ClassicalRegister(1, 'c_reg')\n",
"qc = QuantumCircuit(qr, cr)\n",
"\n",
"qc.h(qr[0])\n",
"\n",
"#Must find state vector for wavefunction before you add measurement operators!\n",
"#(Measurement operators cause wavefunction collapse)\n",
"state_vector = execute(qc,state_vector_sim).result()\n",
"vector = state_vector.get_statevector(qc)\n",
"print('\\nSTATEVECTOR: ', vector)\n",
"\n",
"qc.measure(qr, cr)\n",
"\n",
"print('\\nQUANTUM CIRCUIT DIAGRAM:')\n",
"print(qc.draw())\n",
"print('\\nQASM SPECIFICATION:')\n",
"for i in qc.qasm():\n",
" print(i,end='')\n",
" XXXXXXXXXXn",
"\n",
"print('\\nSIMULATION RESULTS:')\n",
"for i in range(0,3):\n",
" job = execute(qc,simulator,shots=shots)\n",
" result = job.result()\n",
" counts = result.get_counts(qc)\n",
" print('Simulation distribution %d:'%i, counts)\n",
" plt.bar(counts.keys(),counts.values())\n",
" plt.show()\n",
" #plot_histogram(counts, color='midnightblue', title=\"New Histogram\")\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Entanglement and multiqubit gates\n",
"Next, we will experiment with quantum entanglement. The circuit studied will be the Bell State Generator that consists of a Hadamard gate and a CNOT gate."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qr2 = QuantumRegister(2, 'q_reg')\n",
"cr2 = ClassicalRegister(2, 'c_reg')\n",
"qc2 = QuantumCircuit(qr2, cr2)\n",
"qc2.h(qr2[0])\n",
"qc2.cx(qr2[0],qr2[1])\n",
"\n",
"state_vector2 = execute(qc2,state_vector_sim).result()\n",
"vector2 = state_vector2.get_statevector(qc2)\n",
"print('\\nSTATEVECTOR: ', vector2)\n",
"\n",
"qc2.measure(qr2, cr2)\n",
"\n",
"print('\\nQUANTUM CIRCUIT DIAGRAM:')\n",
"print(qc2.draw())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Take note how quantum registers are both initialized to $|0\\rangle$. Therefore, the input to the Bell State Generator is $|00\\rangle$. When we simulate the Bell State Generator, we will get an output quantum state that is entangled. This particular output state is the Bell State $|\\Phi^+\\rangle=\\frac{|00\\rangle + |11\\rangle}{\\sqrt{2}}$. For more information about Bell states, see: https:
en.wikipedia.org/wiki/Bell_state"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('\\nSIMULATION RESULTS:')\n",
"for i in range(0,3):\n",
" job2 = execute(qc2,simulator,shots=shots)\n",
" result2 = job2.result()\n",
" counts2 = result2.get_counts(qc2)\n",
" print('Simulation distribution %d:'%i, counts2)\n",
" state_vector2 = execute(qc2,state_vector_sim).result()\n",
" vector2 = state_vector2.get_statevector(qc2)\n",
" print('Statevector %d:'%i, vector2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The rest of the Bell states, $|\\Phi^-\\rangle$, $|\\Psi^+\\rangle$, $|\\Psi^-\\rangle$, can be generated whenever you change the input state sent into the generator. Input states can be changed by inserting Pauli-X gates on the qubit lines to cause the default initialization of $|0\\rangle$ to evolve into $|1\\rangle$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.1 Question 1: Notice the statevector size is increased. What calculation result in the increase size? What is the growth rate of the statevector with growth of qubits and what does this mean."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Insert the Answer to Question 1 HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 Programming Exercise: Create a bell state generator that results in entangles states `|01> and |10>`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"## Insert your code in this cell, this should generate the entangled state. Your code should also create histograms to verify the output.\n",
"## Note that, for entangled states, it is only necessary to measure ONE of the qubits!!!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Run on real quantum Computer\n",
"\n",
"To use this notebook, you must copy your API token from the
Answered 6 days After Dec 10, 2021

Solution

Sandeep Kumar answered on Dec 16 2021
133 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here