{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Phase Shift\n", "Create a circuit that creates an equal superposition of two qubits, where the phase negative (shifted by $\\pi$) when the two qubits are equal." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit\n", "from qiskit import BasicAer, execute\n", "from qiskit.tools.visualization import plot_histogram" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create quantum/classical registers and a quantum circuit\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Copy the equal2 function from Example 3\n", "\n", "def equal2(circ, input, output) :\n", " circ.cx(input[0],output)\n", " circ.cx(input[1],output)\n", " circ.x(output)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Build the phase shift circuit\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qc.draw(output='mpl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simulate and show results\n", "backend = BasicAer.get_backend('qasm_simulator')\n", "job = execute(qc, backend, shots=512) # shots default = 1024\n", "result = job.result()\n", "print(result.get_counts())\n", "plot_histogram(result.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But where's the phase shift? It's not noticeable in the measurements, because squaring a negative number is the same as squaring its absolute value. So we have to use the **statevector_simulator** instead. There are no measurements allowed, so go back and comment out the measurement -- then skip the box above and run the box below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simulate and show results\n", "backend = BasicAer.get_backend('statevector_simulator')\n", "job = execute(qc, backend) \n", "result = job.result()\n", "print(result.get_statevector())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the state of all three qubits. The output bit shows up as the most significant bit in the state. Since it is always 1 at the end of the circuit, the first four states |0xx> all have amplitude zero. The remaining four states are equally likely, and you can see the negative magnitude for the cases where the output bits are equal: |100> and |111>." ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }