{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Circuit Basics \n",
"\n",
"Here, we provide an overview of working with Qiskit. Qiskit provides the basic building blocks necessary to program quantum computers. The fundamental unit of Qiskit is the [quantum circuit](https://en.wikipedia.org/wiki/Quantum_circuit). A basic workflow using Qiskit consists of two stages: **Build** and **Run**. **Build** allows you to make different quantum circuits that represent the problem you are solving, and **Run** that allows you to run them on different backends. After the jobs have been run, the data is collected and postprocessed depending on the desired output."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-10T11:37:44.387267Z",
"start_time": "2019-08-10T11:37:41.934365Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"from qiskit import QuantumCircuit\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Building the circuit \n",
"\n",
"The basic element needed for your first program is the QuantumCircuit. We begin by creating a `QuantumCircuit` comprised of three qubits."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-10T11:37:44.392806Z",
"start_time": "2019-08-10T11:37:44.389673Z"
}
},
"outputs": [],
"source": [
"# Create a Quantum Circuit acting on a quantum register of three qubits\n",
"circ = QuantumCircuit(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After you create the circuit with its registers, you can add gates (\"operations\") to manipulate the registers. As you proceed through the tutorials you will find more gates and circuits; below is an example of a quantum circuit that makes a three-qubit GHZ state\n",
"\n",
"$$|\\psi\\rangle = \\left(|000\\rangle+|111\\rangle\\right)/\\sqrt{2}.$$\n",
"\n",
"To create such a state, we start with a three-qubit quantum register. By default, each qubit in the register is initialized to $|0\\rangle$. To make the GHZ state, we apply the following gates:\n",
"- A Hadamard gate $H$ on qubit 0, which puts it into the superposition state $\\left(|0\\rangle+|1\\rangle\\right)/\\sqrt{2}$.\n",
"- A controlled-Not operation ($C_{X}$) between qubit 0 and qubit 1.\n",
"- A controlled-Not operation between qubit 0 and qubit 2.\n",
"\n",
"On an ideal quantum computer, the state produced by running this circuit would be the GHZ state above.\n",
"\n",
"In Qiskit, operations can be added to the circuit one by one, as shown below."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-10T11:37:44.401502Z",
"start_time": "2019-08-10T11:37:44.395545Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add a H gate on qubit 0, putting this qubit in superposition.\n",
"circ.h(0)\n",
"# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting\n",
"# the qubits in a Bell state.\n",
"circ.cx(0, 1)\n",
"# Add a CX (CNOT) gate on control qubit 0 and target qubit 2, putting\n",
"# the qubits in a GHZ state.\n",
"circ.cx(0, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize Circuit \n",
"\n",
"You can visualize your circuit using Qiskit `QuantumCircuit.draw()`, which plots the circuit in the form found in many textbooks."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-10T11:37:44.762773Z",
"start_time": "2019-08-10T11:37:44.403727Z"
},
"scrolled": true
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"