{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "# Example 2: Toffoli Gate\n", "In the empty cells below, build the circuit shown on the Example 2 slide. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "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": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# Create quantum registers and classical registers\n", "# Create a circuit (qc) including those registers\n", "\n", "qin = QuantumRegister(2, 'in')\n", "qout = QuantumRegister(1, 'out')\n", "c = ClassicalRegister(1, 'c')\n", "qc = QuantumCircuit(qin,qout,c)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# Add gates to the circuit\n", "# The Toffoli gate is called ccx (controlled-controlled-X) in Qiskit\n", "# Add a measurement to the output bit\n", "\n", "qc.h(qin[0])\n", "qc.h(qin[1])\n", "qc.ccx(qin[0], qin[1], qout[0])\n", "qc.measure(qout,c)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "qc.draw(output='mpl')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "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": { "slideshow": { "slide_type": "skip" } }, "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }