0% found this document useful (0 votes)
67 views

Qiskit Tutorial - By Deep Learning University

Uploaded by

Marcos Vinicius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Qiskit Tutorial - By Deep Learning University

Uploaded by

Marcos Vinicius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

7/19/22, 10:50 AM Qiskit Tutorial - Deep Learning University

Deep Learning University

Qiskit Tutorial
In this Qiskit tutorial, you will be given an introduction to both Quantum Computing and Qiskit. This tutorial will
teach you get you started with the very basics of Quantum Computing- Qubits and how they are different from
their classical counterparts- bits. You will learn about various phenomenon of Quantum Computing such as
Superposition and Entanglement. The tutorial will also cover various Single and Multi Qubit Gates along with
their application to a Qubit in Qiskit. You will learn how to create Quantum Circuits and create Quantum
Programs by using Qiskit. You will learn how to run these Quantum Programs on Quantum Simulators and
measure the results. Additionally, visualization of circuits and results will also be covered in this tutorial. Later in
the tutorial, you will also learn how to run your Quantum Program on a real Quantum Computer provided by
IBM in the cloud. All of this and much more will be covered as a part of this Qiskit Tutorial which aims to teach
you both, Quantum Computing and Qiskit from scratch.

Audience
This Qiskit tutorial is prepared for individuals who are just getting started in the field of Quantum Computing.
This tutorial has been designed to take you from scratch to a moderately advanced level in the field of Quantum
Computing from where you will be able to grasp much more sophisticated concepts yourself. This tutorial will
also make you proficient with using Qiskit.

Prerequisites
This Qiskit tutorial series is designed to get you started with both Quantum Computing and Qiskit. Therefore,
no prior knowledge of either Quantum Computing or Qiskit is required to follow along with this Qiskit tutorial.
However, prior knowledge of Linear Algebra, Matrices, Matrix Multiplication, Vectors, and other basic

https://deeplearninguniversity.com/qiskit/qiskit-tutorial/ 1/3
7/19/22, 10:50 AM Qiskit Tutorial - Deep Learning University

mathematical concepts is necessary to follow along with this tutorial. Additionally, a basic understanding of
Python programming language is also necessary.

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-tutorial/ 2/3
7/19/22, 10:50 AM Qiskit Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-tutorial/ 3/3
7/19/22, 10:53 AM Qiskit Introduction - Deep Learning University

Deep Learning University

Qiskit Introduction
Qiskit is an open-source Software Development Kit (SDK) by IBM for creating Quantum Programs at the level of
Quantum Circuits and running them on IBM Quantum Computers on the cloud, IBM Computers simulating
Quantum Computers in the Cloud, or on local machines simulating Quantum Computers. It is released under
Apache 2.0 license. Qiskit consists of several elements or components such as- Terra, Aer, Ignis, Aqua, IBMQ
Provider, Qiskit, etc. You will learn more about these components in this Qiskit Tutorial Series.

Why learn Quantum Computing?


Computers have revolutionized the world. However, some problems prove to be too difficult for the Computers,
referred to as Classical Computers in the entire tutorial(in the field of Quantum Computing as well). These
problems include various important problems such as Travelling Salesman Problem(TSP), Prime Factorization,
etc. Quantum Computing promises to solve these problems. Quantum Computers can also prove useful in
variety of fields such as Finance, Simulating Nature, Machine Learning(there is an entire field of Quantum
Machine Learning) among others. It is because of its usefulness and its abilities to solve many of these problems
that various organizations such as IBM, Google, Microsoft, along with several other startups are all working in
the field of Quantum Computing.

While the Quantum Computers that are currently available are not sufficient to solve any of these problems. It is
hoped and believed that soon enough the technical challenges associated with Quantum Computing will be
addressed and the Quantum Computers will be able to make themselves helpful for solving real world
problems.

Why learn Qiskit?


Qiskit is developed by IBM Research that allows for easy developing and running Quantum Programs. Qiskit
allows you to run your Quantum Programs on your local machine by simulating a Quantum Computer.
Moreover, IBM offers access to its Quantum Computers and Computers simulating Quantum Computers in the
Cloud free of cost through Qiskit and IBM Quantum- an integrated environment in the cloud that allows users
to develop and run Quantum Programs on the Cloud.

Additionally, Qiskit is a open-source library with strong community support which can be reached out for help.
The Qiskit Community has strong support from IBM. Various competitions, webinars, summer schools, etc are
held by Qiskit through out the year.

https://deeplearninguniversity.com/qiskit/qiskit-introduction/ 1/4
7/19/22, 10:53 AM Qiskit Introduction - Deep Learning University

Since 1981, when the first Physics of Computation Conference was organized at MIT’s Endicott House by MIT
and IBM which led to creation of the field of Quantum Computing, IBM has always maintained its strong
presence in the field. This is true even to this day. All these reasons make a very good case for learning Qiskit.

Do you have Qiskit?


You can check if you already have Qiskit installed by running the following command in your Python interpreter
or Jupyter Notebook.

import qiskit

If you don’t have Qiskit installed, an error message similar to the one below will be displayed.

ModuleNotFoundError: No module named 'qiskit'

However, if no such error message appears, then Qiskit is already installed and you are good to continue with
the rest of this Qiskit tutorial.

Installing Qiskit
Qiskit is easy to install. The procedure for installing it depends on your package manager.

Installing Qiskit using Pip

Pip is the default package management system for python. Therefore, it comes bundled with your python
interpreter and is not required to be installed separately. To install qiskit using pip, simply run the following
command

pip install qiskit

Checking Qiskit Version


You can check the version of Qiskit and all its various components by running the following command

print(qiskit.__qiskit_version__)
https://deeplearninguniversity.com/qiskit/qiskit-introduction/ 2/4
7/19/22, 10:53 AM Qiskit Introduction - Deep Learning University

A sample output for this will be-

{'qiskit-terra': '0.18.2', 'qiskit-aer': '0.8.2', 'qiskit-ignis':


'0.6.0', 'qiskit-ibmq-provider': '0.16.0', 'qiskit-aqua': '0.9.5',
'qiskit': '0.29.1', 'qiskit-nature': None, 'qiskit-finance': None,
'qiskit-optimization': None, 'qiskit-machine-learning': None}

Note– Checking the version of Qiskit by qiskit.__version__ will NOT return the version of Qiskit, but
Qiskit Terra. For example, running the following code

qiskit.__version__

will return the following result-

'0.18.2'

Note– This Qiskit tutorial is written using the Qiskit 0.29.1 version.

Note– It is also recommended that you use Python 3 instead of Python 2 for this tutorial.

With Qiskit installed on your system, you are good to follow along with this Qiskit tutorial.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-introduction/ 3/4
7/19/22, 10:53 AM Qiskit Introduction - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-introduction/ 4/4
7/19/22, 10:55 AM Qiskit: Bra-Ket Notation Tutorial - Deep Learning University

Deep Learning University

Qiskit: Bra-Ket Notation


In this chapter of the Qiskit Tutorial, you will learn about the Bra-Ket Notation. Bra-Ket notation is used
ubiquitously in the field of Quantum Mechanics, Quantum Information, and Quantum Computing.

Ket
A Ket is a column vector. Any column vector a with elements a1 a2 a3 ... an can be written as |a>

Note– |a> is commonly also written as ket(a).

Bra
A Bra is a row vector. Any row vector b with elements b1 b2 b3 ... bn can be written as <b|

Note– <b| is commonly also written as bra(b).

Bras as Hermitian Conjugates of Kets


The Bra is a Hermitian conjugate of its corresponding Ket. For example, Bra- <b| is the hermitian conjugate of
it’s corresponding Ket- |b>. The Hermitian conjugate of a Ket is the transpose of the complex conjugate of its
elements. For a Ket |b> with elements b1 b2 b3 ... bn , the corresponding Bra <b|will have elements b1*
b2* b3* ... bn* . This is further illustrated below.

https://deeplearninguniversity.com/qiskit/qiskit-bra-ket-notation/ 1/4
7/19/22, 10:55 AM Qiskit: Bra-Ket Notation Tutorial - Deep Learning University

Note– The complex conjugate of a complex number can be obtained by reversing the sign of the imaginary
part of the complex number. The complex conjugate of a complex number a + ib is a - ib and that of a -
ib is a + ib. All real numbers(i.e, those without an imaginary part) are their own complex conjugates.

Note– The Hermitian conjugate of a Ket- |b> is also written as |b>†

Example

For a Ket – |r> with elements x1 + i y1 and x2 - i y2

Bra-Ket
Bra-Ket is the multiplication of a Bra(a row vector) and a Ket(a column vector). The product of a Bra and Ket is a
complex number. The Bra-Ket notation of a row vector <b| multiplied by a column vector |a> is <b|a>.

Example

In this example, we will multiply a Bra- <b|with elements b1 and b2 with a Ket- |a> with elements a1 and a2.
The multiplication follows the rules of matrix multiplication. The product of a Bra and Ket is a complex number.
The product of a Bra and a Ket is also called inner product.

Ket-Bra
Ket-Bra is the multiplication of a Ket(a column vector) and a Bra(a row vector). The product of a Ket and Bra is a
matrix. The Ket-Bra notation of a column vector |a> multiplied by a row vector <b| is |a><b|.

Example

https://deeplearninguniversity.com/qiskit/qiskit-bra-ket-notation/ 2/4
7/19/22, 10:55 AM Qiskit: Bra-Ket Notation Tutorial - Deep Learning University

In this example, we will multiply a Ket- |a> with elements a1 and a2 with a Bra- <b|with elements b1 and b2 .
The multiplication follows the rules of matrix multiplication. The product of a Ket and Bra is a matrix. The
product of a Ket and a Bra is also called outer product.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-bra-ket-notation/ 3/4
7/19/22, 10:55 AM Qiskit: Bra-Ket Notation Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-bra-ket-notation/ 4/4
7/19/22, 10:58 AM Classical Computer vs Quantum Computer - Deep Learning University

Deep Learning University

Classical Computer vs Quantum Computer


Before moving further with Quantum Computing and Qiskit, it is important to learn about the differences
between a Classical Computer and a Quantum Computer. In this chapter of the Qiskit Tutorial, you will learn
about the differences between a Classical Computer and a Quantum Computer.

Bit vs Qubit
The fundamental difference between a Quantum Computer and a Classical Computer is at the most basic level
of computation. In Classical Computer, the most basic level of computation is a bit. A bit can either be a 0 or a 1
at any given instance.

In Quantum Computing, we have a Qubit or a Quantum bit, the analogue of bit in Quantum Computing. Like a
bit, a Qubit can either be in the state 0 or 1. However, unlike a bit, it can also be in a state of superposition,
where it is simultaneously both 0 and 1. However, this state of superposition can exist only until the state of the
Qubit is not measured. Once the state of the Qubit is measured, it will collapse into either 0 or 1, with different
probabilities. For example, the state of a Qubit can be such that it is in superposition of states 0 and 1. However,
this state of superposition will collapse into 0 or 1 upon measurement. The possibility with which a Qubit will
collapse into 0 or 1 upon measurement depends on the state of the Qubit.

Bits vs Qubits. Color is for representation purpose only.

Classical Computer vs Quantum Computer


For a Classical Computer, the Input and Output, both are binary. Moreover, during computation as well, the bits
are in either 0 or 1.

https://deeplearninguniversity.com/qiskit/classical-computer-vs-quantum-computer/ 1/4
7/19/22, 10:58 AM Classical Computer vs Quantum Computer - Deep Learning University

For a Quantum Computer, like Classical Computer, the Input and Output are both binary. But during the
computation, the bits are in superposition of 0 and 1. The inputs provided to the Quantum Computer are
binary. During computation, operations are applied on Qubits, like they are on bits, which can cause them to be
in a state of superposition of 0 and 1. However, this state of superposition cannot be measured. Therefore, on
measuring a Qubit, its state collapses into either 0 or 1. The probability with which the Qubit will collapse into 0
or 1 is dependent on the state of the Qubit when measurement is taken place, which in turn depends on the
operations which were applied to the Qubit. These similarities between 0 and 1 are depicted in the figure below.

Comparing Classical and Quantum Computers during Computation, inputs, and outputs.

One important takeaway from the previous section is that the output of a Qubit and therefore Quantum
Computer is probabilistic. But what good can probabilistic outcomes do?

Since the outcome of Qubits is probabilistic and not random, it turns out that it can have many practical
applications. Running the same set of operations multiple times, you can get the Quantum Computers to solve
problems that turn out to be too difficult for classical computers. These problems include problems such as
factoring large numbers, which is used for Public Key Encryption and solving the Travelling Salesman Problem.

The various operations that are applied on Qubits are Quantum Gates. These Quantum Gates perform
operations on the Qubits and change their state. In this tutorial, you will learn about various Quantum Gates
and their effect on the Qubits. You will also learn how you can apply these Quantum Gates on your Qubits in
Qiskit.

However, before moving on with Quantum Gates, it is important to learn how to represent the state of a Qubit.
This is what you will be learning in the next chapter.

https://deeplearninguniversity.com/qiskit/classical-computer-vs-quantum-computer/ 2/4
7/19/22, 10:58 AM Classical Computer vs Quantum Computer - Deep Learning University

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/classical-computer-vs-quantum-computer/ 3/4
7/19/22, 10:58 AM Classical Computer vs Quantum Computer - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/classical-computer-vs-quantum-computer/ 4/4
7/19/22, 10:59 AM Qiskit: Representing a Quantum State Tutorial - Deep Learning University

Deep Learning University

Qiskit: Representing a Quantum State


In previous chapter of this Qiskit Tutorial, you studied that the probability of a state collapsing into 0 or 1 during
measurement depends on the state of the Qubit at the time of measurement. The state of the Qubit is altered
by performing operations on the Qubit such as applying various Quantum Gates. In this chapter of the Qiskit
Tutorial, you will learn about representing a Quantum State such as state of a Qubit.

Representing a Quantum State


A Quantum State is denoted by ψ (psi). Any Quantum State, such as the state of a Qubit can be written in the
form-

The coefficient of |0> (called ket 0), α is a complex number. Similarly, the coefficient of |1> (called ket 1), β is
also a complex number. |0> and |1> are the basis pairs on which the state of the Qubit is measured. You will
learn more about basis pairs in next chapter. For now, it is sufficient to know that any Quantum State can be
measured using these basis pairs- just like every other basis pair, and there are infinite many of them.
Additionally, |0> and |1> are the most common basis pairs for measuring the state of a Qubit. You will get to
know the reason for this in the next section of this chapter.

Note– The state of any Qubit can be described by ψ = α|0> + β|1>. This includes the state of a Qubit in
superposition.

Note– For the mathematically inclined, |0> and |1> (or any basis pairs) are orthonormal vectors.

Probabilities on Measurement
Since any Qubit in the state of superposition collapses into either 0 or 1 upon measurement with different
probabilities depending on the state, the probabilities of measuring 0 and 1 can be obtained from the
representation of the state.

The probability of measuring 0, p(0) and the the probability of measuring 1, p(1) for a Quantum State defined
by ψ = α|0> + β|1> can be calculated as-

https://deeplearninguniversity.com/qiskit/qiskit-representing-a-quantum-state/ 1/4
7/19/22, 10:59 AM Qiskit: Representing a Quantum State Tutorial - Deep Learning University

Measuring a Qubit can result in either measuring a 0 or a 1, therefore-

Note– α and β are also called amplitudes. The square of amplitude gives the measure of probability.

The state ψ = |0> will always result in 0 upon measurement, p(0) = 1. Similarly, the state ψ = |1> will always
result in 1 upon measurement, p(1) = 1.

Example

In this example, we will calculate the probabilities of measuring 0 and 1 upon measurement of a Qubit in the
state ψ = 1/sqrt(2) |0> + 1/sqrt(2) |1>

Note- sqrt(2) represents square-root of 2.

The probability of obtaining 0 on measurement can be calculated as-

Similarly, the probability of obtaining 1 on measurement can be calculated as-

Since the coefficients of both |0> and |1> were equal, the probabilities of obtaining 0 and 1 on measurement
are equal.

Also notice that the probabilities of measuring 0 and 1 add up to 1, as expected.

State as a Vector

https://deeplearninguniversity.com/qiskit/qiskit-representing-a-quantum-state/ 2/4
7/19/22, 10:59 AM Qiskit: Representing a Quantum State Tutorial - Deep Learning University

The state of a Qubit can also be represented as a vector. For a Qubit that has state ψ = α|0> + β|1>,

The state |0> and |1> can be represented as the following vectors-

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-representing-a-quantum-state/ 3/4
7/19/22, 10:59 AM Qiskit: Representing a Quantum State Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-representing-a-quantum-state/ 4/4
7/19/22, 11:05 AM Qiskit: Bloch Sphere Tutorial - Deep Learning University

Deep Learning University

Qiskit: Bloch Sphere


In previous chapter of this Qiskit Tutorial, you learned about representing the state of a Qubit in terms of |0>
and |1> basis pairs. The state of a Qubit can also be represented on a Bloch Sphere. In this chapter of the Qiskit
tutorial, you will learn about the Bloch Sphere, how the states of a Qubit can be represented on the Bloch
Sphere, and how you can plot Bloch Sphere in Qiskit.

Bloch Sphere
Bloch(pronounced as Block) Sphere is a way of representing a Quantum State. It is a sphere with its centre at the
origin and radius of 1 unit. Each point on the surface of the sphere corresponds to a possible Quantum State
that the Qubit can be in. The vector from the centre of the sphere to the point on the sphere represents the
state of the Qubit.

On the Bloch Sphere, the state |0> is on the “north-pole” of the sphere(along the positive z-axis), i.e, at the
coordinate x = 0, y = 0, z = 1. Whereas, the state |1> is on the “south-pole” of the sphere(along the
negative z-axis), i.e, at the coordinate x = 0, y = 0, z = -1.

Similar to the |0> and |1> basis pairs, there are a few other standard basis pairs, such as- |+> and |->, and
|i> and |-i>

https://deeplearninguniversity.com/qiskit/qiskit-bloch-sphere/ 1/5
7/19/22, 11:05 AM Qiskit: Bloch Sphere Tutorial - Deep Learning University

The state |+> has the co-ordinates x = 1, y = 0, z = 0 and the state |-> has the co-ordinates x = -1,
y = 0, z = 0.

Similarly, the state |i> has the co-ordinates x = 0, y = 1, z = 0 and the state |-i> has the co-ordinates
x = 0, y = -1, z = 0.

It is important to note that the Bloch Sphere is a way to represent the state of the Qubit and is not a
representation of the Qubit itself.

Note– The angles on the Bloch Sphere are twice as large. For example, while the vectors |0> and |1> are
orthonormal, the angle between them on the Bloch Sphere is 180°.

Representing a State on Bloch Sphere


Any Quantum State can be represented by a point on the Bloch Sphere. To define any point on the Bloch
Sphere(or any other sphere), we need just 2 parameters.

On the Bloch Sphere, these parameters turn out to be θ- the angle made by the vector from origin to the point
with the positive z-axis.

The other parameter is ɸ- the angle made by the projection of the vector on the xy-plane with the positive x-
axis.

https://deeplearninguniversity.com/qiskit/qiskit-bloch-sphere/ 2/5
7/19/22, 11:05 AM Qiskit: Bloch Sphere Tutorial - Deep Learning University

Note– In last chapter you learned that any Quantum State can be represented as ψ = α|0> + β|1>, where α
and β are complex numbers. The state of the Qubit can also be defined as ψ = α|0> + eiɸ β|1>, where α, β
and ɸ are real numbers instead of complex numbers. Here, ɸ represents the phase of the Qubit(the angle made
between the projection of the vector on xy-plane and positive x-axis). Therefore, the value of both α and β is
dependent on θ. In terms of the value of θ, α = cos(θ/2), and β = sin(θ/2).
Since both α and β are dependent on the value of θ, the probabilities of both, measuring 0 and 1 are
dependent on θ.

The state of a Qubit can be defined in terms of θ and ɸ as-

Where, α, β, ɸ, and θ are all real numbers.

Notice that the equations defining the state of the Qubit define the state in terms of θ/2 and not θ. This is
because the angles on the Bloch Sphere are twice as large.

https://deeplearninguniversity.com/qiskit/qiskit-bloch-sphere/ 3/5
7/19/22, 11:05 AM Qiskit: Bloch Sphere Tutorial - Deep Learning University

Bloch Sphere in Qiskit


You can plot the Bloch Sphere representation of a Qubit using Qiskit. You need to import the
plot_bloch_vector function from qiskit.visualization

You can plot the Bloch Sphere for a Qubit by passing the plot_bloch_vector function the coordinates of the
point on the sphere to which the vector from the centre of the sphere should point. For example, to represent
the state |0> which lies on the ‘north pole’ of the Bloch Sphere, the coordinates of the point x = 0, y = 0,
z = 1 need to be passed to the function as a list. Optionally, the label for the Bloch Sphere diagram can also
be passed with the title parameter.

# Plotting a Bloch Sphere for the state |0>


plot_bloch_vector([0, 0, 1], title="Bloch Sphere for state |0>")

The output of this code will be-

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-bloch-sphere/ 4/5
7/19/22, 11:05 AM Qiskit: Bloch Sphere Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-bloch-sphere/ 5/5
7/19/22, 11:06 AM Standard Basis Pairs - Deep Learning University

Deep Learning University

Standard Basis Pairs


You have already learned about basis pairs- a pair of vector that can be used to represent a Qubit in any state.
There are an infinte number of basis pairs. Basically, any two vectors that make a 180° or π radians angle with
each other on the Bloch Sphere can form a basis pair. The most commonly used basis pairs are |0> and |1>,
however, there are a a few other standard basis pairs that are commonly used. In this chapter of the Qiskit
Tutorial, you will learn about these standard basis pairs.

|0> and |1> Basis Pairs


The first standard basis pair that we will talk about is |0> and |1>, which also happens to be the most
commonly used basis pair. The |0> and |1> basis pairs lie on the Z axis of the Bloch Sphere with |0> lying on
the positive Z axis(the ‘north pole’) and |1> lying on the negative Z axis(the ‘south pole’) of the Bloch Sphere.

The diagrams below show the |0> and |1> basis pairs on a Bloch Sphere.

|+> and |-> Basis Pairs


The |+> and |-> basis pairs is another standard basis pair that is used. The |+> and |-> basis pairs lie on the
X axis of the Bloch Sphere with |+> lying on the positive X axis and |-> lying on the negative X axis of the
Bloch Sphere.

https://deeplearninguniversity.com/qiskit/qiskit-standard-basis-pairs/ 1/4
7/19/22, 11:06 AM Standard Basis Pairs - Deep Learning University

In terms of the |0> and |1> basis pairs, the states |+> and |-> can be described as-

The diagrams below show the |+> and |-> basis pairs on a Bloch Sphere.

|i> and |-i> Basis Pairs


he |i> and |-i> basis pairs is another standard basis pair that is used. The |i> and |-i> basis pairs lie on the
Y axis of the Bloch Sphere with |i> lying on the positive Y axis and |-i> lying on the negative Y axis of the
Bloch Sphere.

In terms of the |0> and |1> basis pairs, the states |i> and |-i> can be described as-

The diagrams below show the |i> and |-i> basis pairs on a Bloch Sphere.
https://deeplearninguniversity.com/qiskit/qiskit-standard-basis-pairs/ 2/4
7/19/22, 11:06 AM Standard Basis Pairs - Deep Learning University

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-standard-basis-pairs/ 3/4
7/19/22, 11:06 AM Standard Basis Pairs - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-standard-basis-pairs/ 4/4
7/19/22, 11:10 AM Qiskit: Born Rule Tutorial - Deep Learning University

Deep Learning University

Qiskit: Born Rule


While Qubits can be in a state of superposition during computing, during measurement, any Qubit that is in a
state of superposition will collapse onto one of the 2 basis pairs. Born Rule is used for predicting the probability
with which a Qubit will collapse onto each of the basis pairs during measurement. While the most commonly
used basis pairs are |0> and |1>, the rule can be used for any basis pairs.

Born Rule
Born Rule states that the probability of a state, say Ψ (psi) collapsing into a state |a>, p(a) when measuring
onto basis pairs |a> and |a>† is given by-

Similarly, the probability of a state Ψ collapsing into state |a>†, p(a†) when measuring onto the same basis
pairs |a> and |a>† is given by-

Alternatively, Since the state Ψ can only collapse into one of the basis pairs- |a> and |a>†, and the sum of
probabilities of collapsing into both states is 1, the probability of a state Ψ collapsing into state |a>† can also be
given by-

Example

In this example, we will calculate the probability of a state Ψ = 1/sqrt(2) |0> + 1/sqrt(2) |1>
collapsing into states |0> and |1> on measurement.

Note– sqrt(2) represents square-root of 2.

https://deeplearninguniversity.com/qiskit/qiskit-born-rule/ 1/4
7/19/22, 11:10 AM Qiskit: Born Rule Tutorial - Deep Learning University

Probability of Getting 0

The probability of Ψ collapsing into state |0> is given by-

After substituting the value of Ψ to 1/sqrt(2) |0> + 1/sqrt(2) |1>, we get-

After simplifying, we get-

Substituting the value of <0|0> and <0|1> as 1 and 0 respectively, we get-

This gives the value of p(0), the probability of state Ψ collapsing into |0> as 0.5

Probability of Getting 1

Similarly, we can calculate the probability of getting |1> on measuring the state Ψ

The probability of Ψ collapsing into state |1> is given by-

After substituting the value of Ψ to 1/sqrt(2) |0> + 1/sqrt(2) |1>, we get-

https://deeplearninguniversity.com/qiskit/qiskit-born-rule/ 2/4
7/19/22, 11:10 AM Qiskit: Born Rule Tutorial - Deep Learning University

After simplifying, we get-

Substituting the value of <1|0> and <1|1> as 0 and 1 respectively, we get-

This gives the value of p(1), the probability of state Ψ collapsing into |1> as 0.5

Notice that the sum of probabilities of the state Ψ collapsing into |0> and |1> during measurement is 1, as
expected.

Also, notice that the probability of the state Ψ collapsing into the state is the square of its amplitude. The
probability of state Ψ = 1/sqrt(2) |0> + 1/sqrt(2) |1> collapsing into state |0> is the square of its
amplitude, 1/sqrt(2). Similarly, the probability of the same state collapsing into state |1> is the square of its
amplitude, 1/sqrt(2).

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-born-rule/ 3/4
7/19/22, 11:10 AM Qiskit: Born Rule Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-born-rule/ 4/4
7/19/22, 11:11 AM Qiskit: Creating a Quantum Circuit - Deep Learning University

Deep Learning University

Qiskit: Creating a Quantum Circuit


Creating a Quantum Circuit is the basic requirement to getting started with working on Quantum Computing.
Creating a Quantum Circuit with Qiskit is as easy as it can get. In this chapter of the Qiskit Tutorial, you will learn
about creating Quantum Circuit and vizualizing it using Qiskit.

Quantum Register
A Quantum Register is a system of many Qubits. A Quantum Register can be created in Qiskit by using the
QuantumRegister class and passing it the number of Qubits required in the Quantum Register.

# Creating a Quantum Register qr with 2 Qubits


qr = QuantumRegister(2)

Classical Register
A Classical Register is a system of many bits. Bits in classical registers are used for storing the measured values
of Qubits in Quantum Registers. A Classical Register can be created in Qiskit by using the
ClassicalRegister class and passing it the number of bits required in the Classical Register.

# Creating a Classical Register cr with 2 bits


cr = ClassicalRegister(2)

Quantum Circuit
A Quantum Circuit consists of Quantum Register and Classical Registers which are used for measuring the states
of Quantum Register. Once the Quantum Circuit is created, it is possible to perform various operations on the
Qubits as well as measuring them. A Quantum Register can be created in Qiskit by using the QuantumCircuit
class and passing it the Quantum Register and Classical Register.

# Creating a Quantum Circuit qc using Quantum Register qr and


Classical Register cr

https://deeplearninguniversity.com/qiskit/qiskit-quantum-circuit/ 1/3
7/19/22, 11:11 AM Qiskit: Creating a Quantum Circuit - Deep Learning University

qc = QuantumCircuit(qr, cr)

Alternatively, the Quantum Circuits can be created by passing the QuantumCircuit class the number of
Qubits and bits required in the Quantum Circuit instead of passing instances of QuantumRegister and
ClassicalRegister class.

# Creating a Quantum Circuit qc 2 Qubits and 2 bits


qc = QuantumCircuit(2, 2)

Visualizing Quantum Circuits


Qiskit allows you to visualize your Quantum Circuits. This is especially needed after performing various
operations on your Quantum Circuit. Visualizing a Quantum Circuit can be done by calling the draw() method
on the Quantum Circuit.

qc.draw()

The draw() method draws the Quantum Circuit. In this particular example, we have not performed any
operation on any Qubit or bit, and therefore, the Quantum Circuit vizualization shows just the Qubits and bits.

Note– Notice that all the Qubits are shown separately(as q_0, q_1), whereas all the bits are clubbed
together(as c) into a Classical Register and the number shown next to it representing the number of bits.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-quantum-circuit/ 2/3
7/19/22, 11:11 AM Qiskit: Creating a Quantum Circuit - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-quantum-circuit/ 3/3
7/19/22, 11:11 AM Measuring a Qubit - Deep Learning University

Deep Learning University

Measuring a Qubit
Once operations are performed on Qubits, it is important to measure their values. However, as you are already
discussed priviously that measuring a Qubit destroys the state of superposition and the state of the Qubit
collapses into either 0 or 1(or more specifically |0> or |1>). Therefore, the Qubits can be measured and their
values can be stored into classical bits. In this chapter of the Qiskit Tutorial, you will learn about measuring a
Qubit in the Quantum Circuit and storing the result in a classical bit.

In this chapter, we will be performing measurements on the following Quantum Circuit-

qr=qiskit.QuantumRegister(2)
cr=qiskit.ClassicalRegister(2)

# Creating a Quantum Circuit from qr and cr


circuit=QuantumCircuit(qr, cr)

The first Qubit, q2_0 has a X Gate operating on it, while the second Qubit q2_1 doesn’t have any gate
operating on it. You will learn about X Gate in later chapters. Having an understanding of the various gates is
not required in this chapter.

Measuring Single Qubit


To measure the state of a single Qubit onto a bit, you can call the measure() method on the Quantum Circuit
and pass it the index of Qubit(which is 0 based) to be measured and the index of classical bit(which is also 0
based) on which the result is to be stored.

Example

In this example, we will measure the value of the 1st Qubit and store the result in the 1st bit.

# Measuring the 1st Qubit(at 0 index) on the 1st bit(at 0 index)


circuit.measure(0, 0)
https://deeplearninguniversity.com/qiskit/measuring-a-qubit/ 1/4
7/19/22, 11:11 AM Measuring a Qubit - Deep Learning University

# Vizualizing the Circuit after measurement


circuit.draw()

Measuring Multiple Qubits


Instead of measuring one Qubit at once, it is possible to measure multiple Qubits at once. To measure multiple
Qubits at once, you can call the measure() method on the Quantum Circuit and pass it the list of indexes of
Qubits you want to measure and the list of indexes of bits you want to measure.

Example

In this example, we will measure the 1st Qubit in the 1st bit and the 2nd Qubit in the 2nd bit.

# Measuring the 1st Qubit in the 1st bit and 2nd Qubit in the 2nd bit
circuit.measure([0, 1], [0, 1])

# Vizualizing the Circuit after measurement


circuit.draw()

Measuring all Qubits


It is also possible to measure all the Qubits in the Quantum Circuit at once. This can be done by calling the
measure_all() method on the Quantum Circuit.

Also note, that when using measure_all() method, you don’t need to have classical bits in your Quantum
Circuit. measure_all() method creates classical bits and write measurements onto them.

Example

https://deeplearninguniversity.com/qiskit/measuring-a-qubit/ 2/4
7/19/22, 11:11 AM Measuring a Qubit - Deep Learning University

In this example, we will create a Quantum Circuit with only Qubits and measure all the Qubits of the Quantum
Circuit by using the measure_all() method.

# Creating a Quantum Circuit with 2 Qubits


circuit = QuantumCircuit(2)

# Adding X Gate on Qubit 0


circuit.x(0)

# Measuring all the Qubits at once by using measure_all method


circuit.measure_all()

# Vizualizing the Circuit after measurement


circuit.draw()

Notice that Qiskit automatically also adds a barrier before measuring the Qubits. You will learn more about this
barrier in later chapters.

Note– While it is not necessary to store the measurement of a qubit at a particular index at the corresponding
index classical bit, but it is a standard practice to do so. For example, the measurement of Qubit at index 0 is
stored in classical bit with index 0, and so on.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/measuring-a-qubit/ 3/4
7/19/22, 11:11 AM Measuring a Qubit - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/measuring-a-qubit/ 4/4
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

Deep Learning University

Qiskit: Information about Quantum Circuit


You can obtain information about a Quantum Circuit by using various methods and attributes of a Quantum
Circuit. In this chapter of the Qiskit tutorial, you will learn about obtaining information about a Quantum Circuit
by using various methods and attributes.

Width of a Quantum Circuit


The width of a Quantum Circuit is the sum of number of Qubits and number of classical bits in a Quantum
Circuit. For example, in a Quantum Circuit with 5 Qubits and 3 bits, the width of the Quantum Circuit will be 8,
the sum of Qubits- 5 and bits- 3.

The width of a Quantum Circuit is also defined as the total number of wires in the diagram of the circuit. For
example, in the below circuit, there are a total of 6 wires, 3 corresponding to 3 Qubits and other 3
corresponding to 3 bits. Hence the total width of the Quantum Circuit is 6.

The width of a Quantum Circuit can be known by calling the width() method on the Quantum Circuit.

Example

In this example, we will consider that qc is the Quantum Circuit that is shown above.

# Storing width of Quantum Circuit in depth


width = qc.width()

print(width)
# Outputs- 6

Size of a Quantum Circuit


https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 1/6
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

The size of a Quantum Circuit is defined as the total number of gates(and also measurements) that are applied
to the Quantum Circuit. For example, for the below circuit, a total of 2 gates and 1 measurement is performed
on the first Qubit. Similarly, for second and third Qubit, a total of 3 gates and 1 gate is applied along with 1
measurement being performed on both the Qubits. This add to a total of 9. Hence the size of the Quantum
Circuit is 9.

The size of a Quantum Circuit can be known by calling the size() method on the Quantum Circuit.

Example

In this example, we will consider that qc is the Quantum Circuit that is shown above.

# Storing size of Quantum Circuit in size


size = qc.size()

print(size)
# Outputs- 9

Depth of a Quantum Circuit


The depth of a Quantum Circuit is defined as the highest number of gates(including measurements) being
applied to any Qubit in the Quantum Circuit. For example, in the circuit below, a total of 3 operations are
applied to the first Qubit(2 Gates and 1 measurement). Similarly, the number of operations being applied to the
second and third Qubits are 4 and 2 respectively. Hence, the depth of the Quantum Circuit is 4- the maximum
among 3, 4, and 2.

https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 2/6
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

The depth of a Quantum Circuit can be known by calling the depth() method on the Quantum Circuit.

Note– The depth() function does not take into consideration the barrier operation.

Example

In this example, we will consider that qc is the Quantum Circuit that is shown above.

# Storing depth of Quantum Circuit in depth


depth = qc.depth()

print(depth)
# Outputs- 4

Number of Classical Bits


The number of classical bits in a Quantum Circuit can be known by using the num_clbits attribute of the
Quantum Circuit.

Example

print(qc.num_clbits)
# Outputs- 3

Number of Qubits
The number of Qubits in a Quantum Circuit can be known by using the num_qubits attribute of the Quantum
Circuit.

Example

https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 3/6
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

print(qc.num_qubits)
# Outputs- 3

Global Phase
The Global Phase of the Quantum Circuit can be known by using the global_phase attribute of the Quantum
Circuit.

Example

print(qc.global_phase)
# Outputs- 0

Operations on Quantum Circuit


The list of the operations that are being performed on a Quantum Circuit can be known by using the data
attribute of the Quantum Circuit.

Example

print(qc.data)

The output for this will be a list of all the operations that are being performed on the Quantum Circuit-

[(<qiskit.circuit.library.standard_gates.h.HGate object at
0x7fb723cfc5d0>, [Qubit(QuantumRegister(3, 'q'), 0)], []),
(<qiskit.circuit.library.standard_gates.s.SGate object at
0x7fb723cfc690>, [Qubit(QuantumRegister(3, 'q'), 1)], []),
(<qiskit.circuit.library.standard_gates.x.XGate object at
0x7fb726f05350>, [Qubit(QuantumRegister(3, 'q'), 2)], []),
(<qiskit.circuit.library.standard_gates.t.TGate object at
0x7fb72635dc90>, [Qubit(QuantumRegister(3, 'q'), 0)], []),
(<qiskit.circuit.library.standard_gates.z.ZGate object at
0x7fb723af0710>, [Qubit(QuantumRegister(3, 'q'), 1)], []),
(<qiskit.circuit.library.standard_gates.s.SGate object at
0x7fb7278b7750>, [Qubit(QuantumRegister(3, 'q'), 1)], []),
(<qiskit.circuit.measure.Measure object at 0x7fb7278b75d0>,
[Qubit(QuantumRegister(3, 'q'), 0)], [Clbit(ClassicalRegister(3, 'c'),
0)]), (<qiskit.circuit.measure.Measure object at 0x7fb725c163d0>,
[Qubit(QuantumRegister(3, 'q'), 1)], [Clbit(ClassicalRegister(3, 'c'),
1)]), (<qiskit.circuit.measure.Measure object at 0x7fb727ba6c50>,
[Qubit(QuantumRegister(3, 'q'), 2)], [Clbit(ClassicalRegister(3, 'c'),
2)])]

https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 4/6
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

Information on Bits
Information on Bits and the order in which they were added can be obtained by using the clbits attribute of
the Quantum Circuit.

Example

print(qc.clbits)

The output of this will be-

[Clbit(ClassicalRegister(3, 'c'), 0),


Clbit(ClassicalRegister(3, 'c'), 1),
Clbit(ClassicalRegister(3, 'c'), 2)]

Information on Qubits
Information on Qubits and the order in which they were added can be obtained by using the qubits attribute
of the Quantum Circuit.

Example

print(qc.qubits)

The output of this will be-

[Qubit(QuantumRegister(3, 'q'), 0),


Qubit(QuantumRegister(3, 'q'), 1),
Qubit(QuantumRegister(3, 'q'), 2)]

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 5/6
7/19/22, 11:16 AM Qiskit: Information about Quantum Circuit - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-information-about-quantum-circuit/ 6/6
7/19/22, 11:23 AM Qiskit: Quantum Gates Tutorial - Deep Learning University

Deep Learning University

Qiskit: Quantum Gates


Like Classical Computing, Quantum Computing also has gates. To differentiate them from their classical
counterparts, we will frequently refer to them as Quantum Gates. Quantum Gates are used for manipulating the
state of a Qubit. A Quantum Gate applied on a Qubit in a particular state can change the state of a Qubit.
Although in some cases, a particular gate might not change the state of a Qubit in a particular state. In this
chapter of the Qiskit Tutorial, you will get a brief overview of the various Single Qubit Quantum Gates. The
following chapters will discuss some important Single Qubit Quantum Gates in detail. Several Quantum Gates
also act on multiple Qubits, these will be covered in a later chapter.

Quantum Gates
Quantum Gates can be represented by a matrix. This matrix can be used to determine the effect of the
Quantum Gate on a particular state. The new state of the Qubit can be known by multiplying the matrix for the
Quantum Gate by the state of the Qubit.

Example†

In the following example, we calculate the new state of a Qubit after the application of X Gate on a Qubit in the
state |0>. The new state ψ is calculated by multiplying the matrix for X Gate, represented by X with the state
vector of the Qubit. The new state of the Qubit is |1>.

Applying X Gate on a Qubit and calculating in output


state.

To summarize, the X Gate changed the state of the Qubit from |0> to |1>

Effect of Quantum Gates


Applying Quantum Gates on a Qubit can have an effect on the state of those Qubits. This change of state can
be represented by the change in the state-vector of the Qubit or its representation on the Bloch Sphere.

Example†

https://deeplearninguniversity.com/qiskit/qiskit-quantum-gates/ 1/5
7/19/22, 11:23 AM Qiskit: Quantum Gates Tutorial - Deep Learning University

In the following example, we consider the effect of the application of the X Gate on its Bloch Sphere
Representation. The original state of the Qubit is |0>. After the application of X Gate, the state of the Qubit
changes to |1>. This change can be seen in the state-vector representation of the Qubit. In the original state
|0>, the state-vector pointed along the positive Z-axis. In the final state |1>, the state-vector pointed along the
negative Z-axis.

Effect of the application of Quantum Gates on the Bloch Sphere Representation of the state of Qubit

† It’s alright if you don’t know what is X Gate. You will get to know about the X Gate and various other gates in
the following chapters. It is sufficient to know for now that applying Quantum Gates on Qubits can change the
state of the Qubit.

Table of Single Qubit Quantum Gates


The following table covers various Single Qubit Quantum Gates in brief.

Quantum Symbol Effect on the Bloch Sphere Comments


Gate

X X Rotates the State vector by π It is also known as NOT Gate and Bit flip gate
radians about the X-axis in anti- because it reverses the probabilities of measuring
clockwise direction. 0 and 1 on measurement. Learn More

Y Y Rotates the State vector by π It is also known as Bit and Phase flip gate
radians about the Y-axis in anti- because it performs both, phase flip like Z Gate
clockwise direction. and bit flip like X Gate. Learn More

Z Z Rotates the State vector by π It is also known as Phase flip gate because it flips
radians about the Z-axis in anti- the phase of a Qubit. Learn More
clockwise direction.

https://deeplearninguniversity.com/qiskit/qiskit-quantum-gates/ 2/5
7/19/22, 11:23 AM Qiskit: Quantum Gates Tutorial - Deep Learning University

Quantum Symbol Effect on the Bloch Sphere Comments


Gate

Identity I or Id No effect on State vector. It is useful when no operation is to be done on a


Qubit. Learn More

Hadamard H Rotates the State vector by π It maps X-axis to Z-axis and vice versa. Learn More
radians about the XZ-axis in
anti-clockwise direction.

RX RX Rotates the State vector by It is a parameterized Gate. It performs rotations


given radians about the X-axis about X-axis by different amounts depending on
in anti-clockwise direction. the parameter. Learn More

RY RY Rotates the State vector by It is a parameterized Gate. It performs rotations


given radians about the Y-axis about Y-axis by different amounts depending on
in anti-clockwise direction. the parameter. Learn More

RZ RZ Rotates the State vector by It is a parameterized Gate. It performs rotations


given radians about the Z-axis about Z-axis by different amounts depending on
in anti-clockwise direction. * the parameter. Learn More

Phase P Rotates the State vector by It is a parameterized Gate. It performs rotations


given radians about the Z-axis about Z-axis by different amounts depending on
in anti-clockwise direction. * the parameter. Learn More

S S Rotates the State vector by π/2 It is also known as the Square-root of Z Gate. This
radians about the Z-axis in anti- is because applying S Gate twice has the same
clockwise direction. effect as a Z Gate. Learn More

S Dagger Sdg or Rotates the State vector by π/2 It is the inverse of S Gate. Learn More
S† radians about the Z-axis in
clockwise direction.

T T Rotates the State vector by π/4 It is also known as the fourth-root of Z Gate. This
radians about the Z-axis in anti- is because applying S Gate four times has the
clockwise direction. same effect as a Z Gate. Learn More

T Dagger Tdg or Rotates the State vector by π/4 It is the inverse of T Gate. Learn More
T† radians about the Z-axis in
clockwise direction.

https://deeplearninguniversity.com/qiskit/qiskit-quantum-gates/ 3/5
7/19/22, 11:23 AM Qiskit: Quantum Gates Tutorial - Deep Learning University

Quantum Symbol Effect on the Bloch Sphere Comments


Gate

SX SX Rotates the State vector by π/2 It is also known as the Square-root of X Gate.
radians about the X-axis in anti- This is because applying SX Gate twice has the
clockwise direction. same effect as an X Gate. Learn More

SX Dagger SXdg or Rotates the State vector by π/2 It is the inverse of SX Gate. Learn More
SX† radians about the X-axis in
clockwise direction.

* The RZ and Phase Gates have the same effect on a Qubit when applied with the same parameter. The
difference between them is that their multi-Qubit versions have different effects. For example, a CP Gate will
have a different effect as compared to a CRZ Gate. You will learn more about this in later chapters.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-quantum-gates/ 4/5
7/19/22, 11:23 AM Qiskit: Quantum Gates Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-quantum-gates/ 5/5
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

Deep Learning University

Qiskit: X Gate
The X Gate is one of the most important gates in Quantum Computing. It is often considered equivalent to the
NOT Gate in Classical Computing. Like many other gates in Quantum Computing, X Gate is a Pauli Gate. Qiskit
provides a method for applying X Gate on a Qubit. In this chapter of the Qiskit Tutorial, you will learn about X
Gate and how to apply X Gate on a Qubit in Qiskit.

X Gate
The X Gate acts on a single Qubit. In terms of its effect on a state represented in terms of the standard basis
pairs |0> and |1>, it maps the coefficient of |0> to |1>, and |1> to |0>. This is results in an exchange of
probabilities of getting |0> and |1>. It is for this reason, that it considered the equivalent of a NOT gate.

Note– The X Gate is also known as Bit flip Gate.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of X Gate will change the
state of the Qubit to Ψ = sqrt(2/3) |0> + 1/sqrt(3) |1>.

Before the X Gate was applied to the Qubit, the probabilities of getting |0> and |1> were 1/3 and 2/3
respectively. After the X Gate was applied on the Qubit, the probabilities of getting |0> and |1> were 2/3 and
1/3 respectively. This is demonstrated in the table below-

Before applying X Gate After applying X Gate

Probability of Getting 0 on 1/3 2/3


measurement

Probability of Getting 1 on 2/3 1/3


measurement

Notice how the probabilities of getting |0> and |1> were exchanged after the X Gate was applied on the
Qubit.

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 1/6
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

X Gate: Bloch Sphere


The X Gate performs a rotation of 180° or π radians about the X-axis on the Bloch Sphere.

Example

In this example, we will look at how the application of X gate on the Qubit affects its representation on the
Bloch Sphere. Remember how X Gate performs a rotation of 180° or π radians about the X-axis on the Bloch
Sphere.

In the below figure, the X Gate is applied on a Qubit in the state Ψ = |0>, and the resulting state of the Qubit
is Ψ = |1>.

Applying X Gate on Qubits in state |0>

In the below figure, the X Gate is applied on a Qubit in the state Ψ = |i> and the resulting state of the Qubit is
Ψ = |-i>.

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 2/6
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

Applying X Gate on Qubits in state |i>

Note– Since X Gate performs rotation about the X-axis, it will have no effect on a vector that lies on the X-axis.

X Gate: Pauli Matrix


The X Gate in Quantum Computing is represented by the matrix X or σx

The resulting state of a Qubit after the application of X Gate can also be calculated by multiplying the Matrix for
X Gate with the vector representing the state of the Qubit.

Example

In this example, we will apply the X gate to a Qubit, and calculate the resulting state by Multiplying it with the
Pauli Matrix for X Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the X Gate, the resulting state of the Qubit can be calculated by-

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 3/6
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

Similarly as before, the probability of getting |0> and |1> are reversed.

Inverse of X Gate
The X is its own inverse. Therefore, applying X Gate to the same Qubit twice will result in the original state of the
Qubit.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of X Gate will change the
state of the Qubit to Ψ = sqrt(2/3) |0> + 1/sqrt(3) |1>.

After application of the X Gate again to the same Qubit, the resulting state of the Qubit will be Ψ =
1/sqrt(3) |0> + sqrt(2/3) |1>.

Since applying X Gate twice to the same Qubit results in the same state, X Gate is its own inverse.

X Gate in Qiskit
The X Gate in Qiskit can be applied to any Qubit by calling the x() method on the Quantum Circuit(an instance
of QuantumCircuit class) and passing it an integer for the Qubit on which X Gate is to be applied.

Example

In this example, we will be applying X Gate on the first Qubit in the Quantum Circuit, which will contain 2
Qubits.

# Creating a Quantum Register with 2 Qubit


qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit


circuit = QuantumCircuit(qr)

# Applying X Gate on the first Qubit


circuit.x(0)

# Drawing the Quantum Circuit


circuit.draw()

This will result in the following Quantum Circuit being drawn-

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 4/6
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

Notice that the X Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 5/6
7/19/22, 11:25 AM Qiskit: X Gate Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-x-gate/ 6/6
7/19/22, 11:26 AM Qiskit: Y Gate Tutorial - Deep Learning University

Deep Learning University

Qiskit: Y Gate
The Y Gate is an important gate in Quantum Computing. Like many other gates in Quantum Computing, Y Gate
is a Pauli Gate. Qiskit provides a method for applying Y Gate on a Qubit. In this chapter of the Qiskit Tutorial,
you will learn about Y Gate and how to apply Y Gate on a Qubit in Qiskit.

Y Gate
The Y Gate acts on a single Qubit. In terms of its effect on a state represented in terms of the standard basis
pairs |0> and |1>, it maps the coefficient of |0> to |1>, and |1> to |0>. This is results in an exchange of
probabilities of getting |0> and |1>. Additionally, it also flips the phase of the Qubit by 180° or π radians.

Note– The Y Gate is also known as Bit and Phase flip Gate. This is because it performs a bit flip like X Gate and a
Phase flip like Z Gate.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Y Gate will change the state
of the Qubit to Ψ = -i sqrt(2/3) |0> + i/sqrt(3) |1>.

Before the Y Gate was applied to the Qubit, the probabilities of getting |0> and |1> were 1/3 and 2/3
respectively. After the Y Gate was applied on the Qubit, the probabilities of getting |0> and |1> were 2/3 and
1/3 respectively. Hence, there was an interchange in the probabilities of getting |0> and |1>.This is
demonstrated in the table below-

Before applying Y Gate After applying Y Gate

Probability of Getting 0 on 1/3 2/3


measurement

Probability of Getting 1 on 2/3 1/3


measurement

There is also a change in the phase of the Qubit by 180° or π radians which can be noticed with the change of
sign for the coefficient of |0>.

https://deeplearninguniversity.com/qiskit/qiskit-y-gate/ 1/5
7/19/22, 11:26 AM Qiskit: Y Gate Tutorial - Deep Learning University

Y Gate: Bloch Sphere


The Y Gate performs a rotation of 180° or π radians about the Y-axis on the Bloch Sphere.

Example

In this example, we will look at how the application of Y gate on the Qubit affects its representation on the
Bloch Sphere. Remember how Y Gate performs a rotation of 180° or π radians about the Y-axis on the Bloch
Sphere.

In the below figure, the Y Gate is applied on a Qubit in the state Ψ = |0>, and the resulting state of the Qubit
is Ψ = |1>.

In the below figure, the Y Gate is applied on a Qubit in the state Ψ = |+> and the resulting state of the Qubit is
Ψ = |->.

Note– Since Y Gate performs rotation about the Y-axis, it will have no effect on a vector that lies on the Y-axis.

https://deeplearninguniversity.com/qiskit/qiskit-y-gate/ 2/5
7/19/22, 11:26 AM Qiskit: Y Gate Tutorial - Deep Learning University

Y Gate: Pauli Matrix


The Y Gate in Quantum Computing is represented by the matrix Y or σy

The resulting state of a Qubit after the application of Y Gate can also be calculated by multiplying the Matrix for
Y Gate with the vector representing the state of the Qubit.

Example

In this example, we will apply the Y gate to a Qubit, and calculate the resulting state by Multiplying it with the
Pauli Matrix for Y Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the Y Gate, the resulting state of the Qubit can be calculated by-

Similarly as before, the probability of getting |0> and |1> are reversed along with a flip in phase.

Inverse of Y Gate
The Y is its own inverse. Therefore, applying Y Gate to the same Qubit twice will result in the original state of the
Qubit.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Y Gate will change the state
of the Qubit to Ψ = -i sqrt(2/3) |0> + i/sqrt(3) |1>.

After application of the Y Gate again to the same Qubit, the resulting state of the Qubit will be Ψ =
1/sqrt(3) |0> + sqrt(2/3) |1>.

https://deeplearninguniversity.com/qiskit/qiskit-y-gate/ 3/5
7/19/22, 11:26 AM Qiskit: Y Gate Tutorial - Deep Learning University

Since applying Y Gate twice to the same Qubit results in the same state, Y Gate is its own inverse.

Y Gate in Qiskit
The Y Gate in Qiskit can be applied to any Qubit by calling the y() method on the Quantum Circuit(an instance
of QuantumCircuit class) and passing it an integer for the Qubit on which Y Gate is to be applied.

Example

In this example, we will be applying Y Gate on the first Qubit in the Quantum Circuit, which will contain 2
Qubits.

# Creating a Quantum Register with 1 Qubit


qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit


circuit = QuantumCircuit(qr)

# Applying Y Gate on the first Qubit


circuit.y(0)

# Drawing the Quantum Circuit


circuit.draw()

This will result in the following Quantum Circuit being drawn-

Notice that the Y Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-y-gate/ 4/5
7/19/22, 11:26 AM Qiskit: Y Gate Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-y-gate/ 5/5
7/19/22, 11:27 AM Qiskit: Z Gate Tutorial - Deep Learning University

Deep Learning University

Qiskit: Z Gate
The Z Gate is an important gate in Quantum Computing. Like many other gates in Quantum Computing, Z Gate
is a Pauli Gate. Qiskit provides a method for applying Z Gate on a Qubit. In this chapter of the Qiskit Tutorial,
you will learn about Z Gate and how to apply Z Gate on a Qubit in Qiskit.

Z Gate
The Z Gate acts on a single Qubit. In terms of its effect on a state represented in terms of the standard basis
pairs |0> and |1>, it has no effect on the coefficients of |0> to |1>. Hence, the application of Z Gate does not
alter the probabilities of the state of the Qubit collapsing into |0> or |1> upon measurement. However, the Z
Gate does flip the phase of the Qubit by 180° or π radians.

Note– The Z Gate is also known as Phase flip Gate.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Z Gate will change the state
of the Qubit to Ψ = 1/sqrt(3) |0> - sqrt(2/3) |1>.

Before the Z Gate was applied to the Qubit, the probabilities of getting |0> and |1> were 1/3 and 2/3
respectively. After the Z Gate was applied on the Qubit, the probabilities of getting |0> and |1> were the same.
Therefore, there was no change in the probabilities of getting |0> and |1>.This is demonstrated in the table
below-

Before applying Z Gate After applying Z Gate

Probability of Getting 0 on 1/3 1/3


measurement

Probability of Getting 1 on 2/3 2/3


measurement

Although, there is a change in the phase of the Qubit by 180° or π radians which can be noticed with the
change of sign for the coefficient of |1>.

https://deeplearninguniversity.com/qiskit/qiskit-z-gate/ 1/5
7/19/22, 11:27 AM Qiskit: Z Gate Tutorial - Deep Learning University

Z Gate: Bloch Sphere


The Z Gate performs a rotation of 180° or π radians about the Z-axis on the Bloch Sphere.

Example

In this example, we will look at how the application of Z gate on the Qubit affects its representation on the
Bloch Sphere. Remember how Z Gate performs a rotation of 180° or π radians about the Z-axis on the Bloch
Sphere.

In the below figure, the Z Gate is applied on a Qubit in the state Ψ = |i>, and the resulting state of the Qubit
is Ψ = |-i>.

In the below figure, the Z Gate is applied on a Qubit in the state Ψ = |+> and the resulting state of the Qubit is
Ψ = |->.

Note– Since Z Gate performs rotation about the Z-axis, it will have no effect on a vector that lies on the Z-axis.

https://deeplearninguniversity.com/qiskit/qiskit-z-gate/ 2/5
7/19/22, 11:27 AM Qiskit: Z Gate Tutorial - Deep Learning University

Z Gate: Pauli Matrix


The Z Gate in Quantum Computing is represented by the matrix Z or σz

The resulting state of a Qubit after the application of Z Gate can also be calculated by multiplying the Matrix for
Z Gate with the vector representing the state of the Qubit.

Example

In this example, we will apply the Z gate to a Qubit, and calculate the resulting state by Multiplying it with the
Pauli Matrix for Z Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the Z Gate, the resulting state of the Qubit can be calculated by-

Similarly as before, the probability of getting |0> and |1> are the same whereas there is a flip in the phase.

Inverse of Z Gate
The Z is its own inverse. Therefore, applying Z Gate to the same Qubit twice will result in the original state of the
Qubit.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Z Gate will change the state
of the Qubit to Ψ = 1/sqrt(3) |0> - sqrt(2/3) |1>.

After application of the Z Gate again to the same Qubit, the resulting state of the Qubit will be Ψ =
1/sqrt(3) |0> + sqrt(2/3) |1>.

Since applying Z Gate twice to the same Qubit results in the same state, Z Gate is its own inverse.

https://deeplearninguniversity.com/qiskit/qiskit-z-gate/ 3/5
7/19/22, 11:27 AM Qiskit: Z Gate Tutorial - Deep Learning University

Z Gate in Qiskit
The Z Gate in Qiskit can be applied to any Qubit by calling the z() method on the Quantum Circuit(an instance
of QuantumCircuit class) and passing it an integer for the Qubit on which Z Gate is to be applied.

Example

In this example, we will be applying Z Gate on the first Qubit in the Quantum Circuit, which will contain 2
Qubits.

# Creating a Quantum Register with 1 Qubit


qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit


circuit = QuantumCircuit(qr)

# Applying Z Gate on the first Qubit


circuit.z(0)

# Drawing the Quantum Circuit


circuit.draw()

This will result in the following Quantum Circuit being drawn-

Notice that the Z Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-z-gate/ 4/5
7/19/22, 11:27 AM Qiskit: Z Gate Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-z-gate/ 5/5
7/19/22, 11:29 AM Qiskit: Identity Gate Tutorial - Deep Learning University

Deep Learning University

Qiskit: Identity Gate


The Identity Gate is one of the seemingly un-important Gate in Quantum Computing. Qiskit provides a method
for applying Identity Gate on a Qubit. In this chapter of the Qiskit Tutorial, you will learn about Identity Gate and
how to apply the Identity Gate on a Qubit in Qiskit.

Identity Gate
The Identity Gate or I Gate acts on a single Qubit. The I Gate does nothing.

It is however used for calculations. For example, in the equations below the I Matrix(equivalent to the
application of I Gate) is used for proving that X, Y, Z, and H gates are their own inverse.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of I Gate will keep the state of
the Qubit to Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>. Therefore, there is no change- neither in the
amplitude/ probability nor the phase in the state of the Qubit after applying the I Gate.

Identity Gate: Bloch Sphere


Since the I Gate does not change the state of a Qubit, the Bloch Sphere representation of the Qubit does not
change after the application of I Gate.

Identity Gate: Pauli Matrix


https://deeplearninguniversity.com/qiskit/qiskit-identity-gate/ 1/4
7/19/22, 11:29 AM Qiskit: Identity Gate Tutorial - Deep Learning University

The I Gate in Quantum Computing is represented by the matrix I which is an identity matrix.

Since the matrix representation of the I Gate is an Identity Matrix. Multiplying the Matrix for I Gate with the
vector representing the state of the Qubit will result in the original vector.

Example

In this example, we will apply the I gate to a Qubit, and calculate the resulting state by Multiplying it with the
Matrix for I Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the I Gate, the resulting state of the Qubit can be calculated by-

Similarly as before, the state of the Qubit does not change after the application of I Gate.

Identity Gate in Qiskit


The I Gate in Qiskit can be applied to any Qubit by calling the i() method on the Quantum Circuit(an instance
of QuantumCircuit class) and passing it an integer for the Qubit on which I Gate is to be applied.

Example

In this example, we will be applying I Gate on the first Qubit in the Quantum Circuit, which will contain 2 Qubits.

# Creating a Quantum Register with 1 Qubit


qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit


circuit = QuantumCircuit(qr)

# Applying I Gate on the first Qubit


circuit.i(0)

https://deeplearninguniversity.com/qiskit/qiskit-identity-gate/ 2/4
7/19/22, 11:29 AM Qiskit: Identity Gate Tutorial - Deep Learning University

# Drawing the Quantum Circuit


circuit.draw()

This will result in the following Quantum Circuit being drawn-

Notice that the I Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-identity-gate/ 3/4
7/19/22, 11:29 AM Qiskit: Identity Gate Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-identity-gate/ 4/4
7/19/22, 11:31 AM Qiskit: Hadamard Gate - Deep Learning University

Deep Learning University

Qiskit: Hadamard Gate


The Hadamard Gate is one of the most important gates in Quantum Computing. Qiskit provides a method for
applying Hadamard Gate on a Qubit. In this chapter of the Qiskit Tutorial, you will learn about Hadamard Gate
and how to apply the Hadamard Gate on a Qubit in Qiskit.

Hadamard Gate
The Hadamard Gate or sometimes also known as H Gate acts on a single Qubit. The Hadamard Gate can be
used to create a state of superposition in a Qubit. For this reason, Hadamard Gate is sometimes also known as
the Gateway to Quantum realm.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Hadamard Gate will change
the state of the Qubit to Ψ = 0.986 |0> - 0.169 |1> (approximated).

The application of Hadamard Gate usually results in a change of both, phase and probabilities.

Hadamard Gate: Bloch Sphere


The Hadamard Gate performs a rotation of 180° or π radians about the XZ-axis(the axis making a 45° angle with
both X axis and Z axis) on the Bloch Sphere.

Example

In this example, we will look at how the application of Hadamard gate on the Qubit affects its representation on
the Bloch Sphere. Remember how Hadamard Gate performs a rotation of 180° or π radians about the XZ-axis
on the Bloch Sphere.

In the below figure, the Hadamard Gate is applied on a Qubit in the state Ψ = |0>, and the resulting state of
the Qubit is Ψ = |+>.

https://deeplearninguniversity.com/qiskit/qiskit-hadamard-gate/ 1/5
7/19/22, 11:31 AM Qiskit: Hadamard Gate - Deep Learning University

Applying Hadamard Gate on Qubits in state |0>

In the below figure, the Hadamard Gate is applied on a Qubit in the state Ψ = |1> and the resulting state of
the Qubit is Ψ = |->.

Applying Hadamard Gate on Qubits in state |1>

Note– Since Hadamard Gate performs rotation about the XZ-axis, it will have no effect on a vector that lies on
the XZ-axis.

Hadamard Gate: Matrix


The Hadamard Gate in Quantum Computing is represented by the matrix H

The resulting state of a Qubit after the application of Hadamard Gate can also be calculated by multiplying the
Matrix for Hadamard Gate with the vector representing the state of the Qubit.

https://deeplearninguniversity.com/qiskit/qiskit-hadamard-gate/ 2/5
7/19/22, 11:31 AM Qiskit: Hadamard Gate - Deep Learning University

Example

In this example, we will apply the Hadamard gate to a Qubit, and calculate the resulting state by Multiplying it
with the Matrix for the Hadamard Gate.

Let the state of the Qubit be Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>

After Applying the Hadamard Gate, the resulting state of the Qubit can be calculated by-

Notice the change in both, probabilities and phase on the application of Hadamard Gate.

Inverse of Hadamard Gate


The Hadamard is its own inverse. Therefore, applying Hadamard Gate to the same Qubit twice will result in the
original state of the Qubit.

Example

For a Qubit in state Ψ = 1/sqrt(3) |0> + sqrt(2/3) |1>, the application of Hadamard Gate will change
the state of the Qubit to Ψ = 0.986 |0> - 0.169 |1>.

After application of the Hadamard Gate again to the same Qubit, the resulting state of the Qubit will be Ψ =
1/sqrt(3) |0> + sqrt(2/3) |1>.

Since applying Hadamard Gate twice to the same Qubit results in the same state, Hadamard Gate is its own
inverse.

Hadamard Gate in Qiskit


The Hadamard Gate in Qiskit can be applied to any Qubit by calling the h() method on the Quantum Circuit(an
instance of QuantumCircuit class) and passing it an integer for the Qubit on which Hadamard Gate is to be
applied.

Example

In this example, we will be applying Hadamard Gate on the first Qubit in the Quantum Circuit, which will contain
2 Qubits.

https://deeplearninguniversity.com/qiskit/qiskit-hadamard-gate/ 3/5
7/19/22, 11:31 AM Qiskit: Hadamard Gate - Deep Learning University

# Creating a Quantum Register with 1 Qubit


qr = qiskit.QuantumRegister(2)

# Creating a Quantum Circuit


circuit = QuantumCircuit(qr)

# Applying Hadamard Gate on the first Qubit


circuit.h(0)

# Drawing the Quantum Circuit


circuit.draw()

This will result in the following Quantum Circuit being drawn-

Notice that the Hadamard Gate is applied to the first Qubit. Also notice, that Qubits follow a 0 based indexing.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-hadamard-gate/ 4/5
7/19/22, 11:31 AM Qiskit: Hadamard Gate - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-hadamard-gate/ 5/5
7/19/22, 11:33 AM Qiskit: Running Quantum Programs Tutorial - Deep Learning University

Deep Learning University

Qiskit: Running Quantum Programs


You have already learned how to create Quantum Circuits using Qiskit. In this chapter of the Qiskit Tutorial, you
will learn how to run your Quantum Program. You will be running these programs on a Classical Machine which
will simulate an Error Correcting Quantum Computer. Though no Error Correcting Quantum Computer exists
anywhere in the world today, it is expected that they will soon be a reality. In later chapters of this tutorial you
will also run your Quantum Program on an actual Quantum Computer by IBM.

Running a Quantum Program


You will now create a Quantum Program and run it on a simulator on your local machine.

Qiskit Aer

Qiskit Aer provides an interface to run Quantum Programs locally on your machine. Therefore Aer needs to be
imported first.

# Importing Aer
from qiskit import Aer

Qiskit Aer provides a interface for many backends which simulate a Quantum Computer. These backends can be
explored by calling the backends() function on Aer.

Aer.backends()

This will output the list of available backends.

https://deeplearninguniversity.com/qiskit/qiskit-running-quantum-programs/ 1/5
7/19/22, 11:33 AM Qiskit: Running Quantum Programs Tutorial - Deep Learning University

Available Backends on Aer

In this chapter, we will be making use of Aer Simulator(class AerSimulator).

Creating Quantum Circuit

Additionally, we also need to import the QuantumCircuit class which is needed for building Quantum
Circuits.

# Importing QuantumCircuit
from qiskit import QuantumCircuit

Next, we will create a Quantum Circuit with one Qubit and one bit.

# Creating a Quantum Circuit with 1 Qubit and 1 bit


qc = QuantumCircuit(1, 1)

# Measuring the value of Qubit onto classical bit


qc.measure(0, 0)

Note– The state of the Qubit is by default initialized to 0. In this Quantum Program we are just measuring the
state of the Qubit.

Running Simulation

Everything is set for running the Quantum Program we just built. To run the quantum program, first choose a
simulator.

simulator = Aer.get_backend('aer_simulator')

Next, we run the program on the Simulator and get the results.
https://deeplearninguniversity.com/qiskit/qiskit-running-quantum-programs/ 2/5
7/19/22, 11:33 AM Qiskit: Running Quantum Programs Tutorial - Deep Learning University

result = simulator.run(qc).result()

result contains information about the program that was just run on the Simulator. The simulator runs the
Quantum Program a given number of times, by default 1024. The number of times the Quantum Program needs
to run can be passed to the run() function as shots parameter. The number of times the measurement of
Qubit results in 0 and 1 is returned by the get_counts() function.

counts = result.get_counts()

The output of get_counts() for this example is-

Note– Notice that the result of running the program 1024 times results in 0 all the times. This is what was
expected, because the Qubits are initialized in the |0> state, and no operation was performed on the Qubit
before measuring it. Hence, the measurement will result in 0 every time.

You can initialize a Qubit into any state you want. Learn more about Initializing a Qubit

Visualizing the Output

The output of the Quantum Program can be vizualized in various forms such as Histograms, Q-Sphere,
CityScape, etc. You will be learning about plotting various vizualizations by using Qiskit in a later chapter. In this
chapter, we will just be vizualizing the outputs with the help of a histogram.

To plot histogram, plot_histogram() function needs to be imported from qiskit.visualization

# Importing plot_histogram function


from qiskit.visualization import plot_histogram

Next, pass the the counts variable to plot_histogram() function. This will plot the histogram for the
outputs of the Quantum Program.

plot_histogram(counts)

The output of this will be-

https://deeplearninguniversity.com/qiskit/qiskit-running-quantum-programs/ 3/5
7/19/22, 11:33 AM Qiskit: Running Quantum Programs Tutorial - Deep Learning University

Note– As discussed above, the Qubits are initialized in state |0>. Hence, measuring a Qubit without performing
any operation will always result in 0. For this reason, the probability of measuring 0 is 1.

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-running-quantum-programs/ 4/5
7/19/22, 11:33 AM Qiskit: Running Quantum Programs Tutorial - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-running-quantum-programs/ 5/5
7/19/22, 12:23 PM Qiskit: Coin Tossing - Deep Learning University

Deep Learning University

Qiskit: Coin Tossing


In previous chapter, you have already learned about running Quantum Programs by using Quantum Simulators
such as Qiskit Aer Simulator. In this chapter, you will create your a Quantum Program to generate a random
number from 0 and 1, the equivalent to tossing a coin.

This program is commonly referred to as the “Hello World” of Quantum Computing. While it may not look like
much, this program will do that no Classical Computer can do- generating a Random Number. This is because
all the algorithms for generating a random number on a classical computer are deterministic. However, this will
only be true when running this Quantum Program on an actual Quantum Computer and not a simulator, which
we will do in one of the coming chapters.

Quantum Coin Tossing Program


We will first create a Quantum Program for the equivalent of tossing a coin, and then run the experiments a
certain number of times and measure the outputs.

Creating the Quantum Program

We will first create a Quantum Circuit with 1 Qubit and 1 bit.

# Creating a Quantum Circuit with 1 Qubit and 1 bit


qc = QuantumCircuit(1, 1)

Next, we apply the Hadamard Gate(H Gate) to the Qubit, which changes the state of the Qubit from |0> to a
state of superposition 1/sqrt(2) |0> + 1/sqrt(2) |1>. In this state of superposition, the probability of
obtaining both 0 and 1 upon measurement is equal. Therefore, we measure the Qubit in this state of
superposition.

# Applying Hadamard Gate on the first Qubit


qc.h(0)

# Measuring the state of the Qubit onto classical bit


qc.measure(0, 0)

https://deeplearninguniversity.com/qiskit/qiskit-tossing-a-coin/ 1/4
7/19/22, 12:23 PM Qiskit: Coin Tossing - Deep Learning University

Before running the Quantum Program, let’s visualize the circuit.

# Drawing the Circuit


qc.draw()

The output of this will be-

Running the Quantum Program

We will now run the Quantum Program for a total of 10,000 times.

# Creating the Simulator


simulator = Aer.get_backend('aer_simulator')

# Running the Quantum Program for 10000 times


results = simulator.run(qc, shots=10000).result()

# Getting the counts of 0 and 1 in the results


counts = results.get_counts()

# printing counts
print(counts)

The output of this will be-

Note– Even though the probability of both 0 and 1 is 0.5 each, the number of times they actually occur when
running the program for 10,000 times does NOT have to 5000 each. As the number of times the program
runs(shots parameter) keeps on increasing, the number of heads and tails actually occurring keep on moving
close to their actual probabilities. This effect is known as the Law of Large Numbers.

Plotting Histogram

Lets plot the histogram for the results now.

# Plot Histogram
plot_histogram(counts)

https://deeplearninguniversity.com/qiskit/qiskit-tossing-a-coin/ 2/4
7/19/22, 12:23 PM Qiskit: Coin Tossing - Deep Learning University

The output of this will be-

❮ Previous

Next ❯

https://deeplearninguniversity.com/qiskit/qiskit-tossing-a-coin/ 3/4
7/19/22, 12:23 PM Qiskit: Coin Tossing - Deep Learning University

Copyright © 2022 Deep Learning University | Powered by Astra WordPress Theme

https://deeplearninguniversity.com/qiskit/qiskit-tossing-a-coin/ 4/4

You might also like