{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 3: Two-qubit equality circuit\n", "Create a circuit that checks whether two qubits are equal in the computational basis. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit\n", "from qiskit import Aer, execute\n", "from qiskit.tools.visualization import plot_histogram" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a 2-qubit input register and a 1-qubit output register\n", "# Create a 1-bit classical register for the measurement`\n", "# Create a circuit (qc) including those registers\n", "\n", "qin = QuantumRegister(2) # input\n", "qout = QuantumRegister(1) # output\n", "c = ClassicalRegister(1)\n", "qc = QuantumCircuit(qin,qout,c)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define a python function that takes a circuit, an input register, and an output register\n", "# The function adds gates to the circuit to implement the equals circuit\n", "\n", "def equal2(circ, input, output) :\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Build a circuit that initializes the input bits to |0> or |1> (using an X gate)\n", "# Calls the equal2 function and then measures the output\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 = Aer.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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }