0% found this document useful (0 votes)
80 views10 pages

Quantum Computing Lesson 1-3

Uploaded by

jlesalvador
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views10 pages

Quantum Computing Lesson 1-3

Uploaded by

jlesalvador
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

7/23/24, 12:54 PM quantum computing lesson 1

In [1]: import numpy as np

In [4]: ket0 = np.array([1,0])


ket1 = np.array([0,1])
print(1/2*ket0 + 1/2*ket1)

[0.5 0.5]

In [9]: M1 = np.array([[1,1],[0,0]])
M2 = np.array([[1,1],[1,0]])
print(M1)
print()
print(M2)
M1/2 + M2/2

[[1 1]
[0 0]]

[[1 1]
[1 0]]
Out[9]: array([[1. , 1. ],
[0.5, 0. ]])

In [12]: M1.dot(ket1)

Out[12]: array([1, 0])

In [14]: np.matmul(M1,ket1)

Out[14]: array([1, 0])

In [16]: M1.dot(M2)

Out[16]: array([[2, 1],


[0, 0]])

In [17]: M2.dot(M1)

Out[17]: array([[1, 1],


[1, 1]])

In [18]: # Install Qiskit

!pip install qiskit

file:///home/aman/Downloads/quantum computing lesson 1.html 1/10


7/23/24, 12:54 PM quantum computing lesson 1

Collecting qiskit
Obtaining dependency information for qiskit from https://files.pythonhoste
d.org/packages/33/cf/91090452d62e661ef982ca9170b0aedc5ce793ef1cd6cd9ed6d3809
d4cc6/qiskit-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
metadata
Downloading qiskit-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86
_64.whl.metadata (12 kB)
Collecting rustworkx>=0.14.0 (from qiskit)
Obtaining dependency information for rustworkx>=0.14.0 from https://files.
pythonhosted.org/packages/e7/09/5ef178a2df03796bdbe3f9961071445d81c52cece4d0
a42f22ac8fec3f8d/rustworkx-0.14.1-cp311-cp311-manylinux_2_17_x86_64.manylinu
x2014_x86_64.whl.metadata
Downloading rustworkx-0.14.1-cp311-cp311-manylinux_2_17_x86_64.manylinux20
14_x86_64.whl.metadata (10.0 kB)
Requirement already satisfied: numpy<2,>=1.17 in ./anaconda3/lib/python3.11/
site-packages (from qiskit) (1.24.3)
Requirement already satisfied: scipy>=1.5 in ./anaconda3/lib/python3.11/site
-packages (from qiskit) (1.11.1)
Requirement already satisfied: sympy>=1.3 in ./anaconda3/lib/python3.11/site
-packages (from qiskit) (1.11.1)
Requirement already satisfied: dill>=0.3 in ./anaconda3/lib/python3.11/site-
packages (from qiskit) (0.3.6)
Requirement already satisfied: python-dateutil>=2.8.0 in ./anaconda3/lib/pyt
hon3.11/site-packages (from qiskit) (2.8.2)
Collecting stevedore>=3.0.0 (from qiskit)
Obtaining dependency information for stevedore>=3.0.0 from https://files.p
ythonhosted.org/packages/eb/f1/c7c6205c367c764ee173537f7eaf070bba4dd0fa11bf0
81813c2f75285a3/stevedore-5.2.0-py3-none-any.whl.metadata
Downloading stevedore-5.2.0-py3-none-any.whl.metadata (2.3 kB)
Requirement already satisfied: typing-extensions in ./anaconda3/lib/python3.
11/site-packages (from qiskit) (4.7.1)
Collecting symengine>=0.11 (from qiskit)
Obtaining dependency information for symengine>=0.11 from https://files.py
thonhosted.org/packages/3b/50/0f6635c91c20322f9f79d0061421b4bca2b5175d2158d1
9e2c4f1a669ce8/symengine-0.11.0-cp311-cp311-manylinux_2_12_x86_64.manylinux2
010_x86_64.whl.metadata
Downloading symengine-0.11.0-cp311-cp311-manylinux_2_12_x86_64.manylinux20
10_x86_64.whl.metadata (1.2 kB)
Requirement already satisfied: six>=1.5 in ./anaconda3/lib/python3.11/site-p
ackages (from python-dateutil>=2.8.0->qiskit) (1.16.0)
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore>=3.0.0->qiskit)
Obtaining dependency information for pbr!=2.1.0,>=2.0.0 from https://file
s.pythonhosted.org/packages/64/dd/171c9fb653591cf265bcc89c436eec75c9bde3dec9
21cc236fa71e5698df/pbr-6.0.0-py2.py3-none-any.whl.metadata
Downloading pbr-6.0.0-py2.py3-none-any.whl.metadata (1.3 kB)
Requirement already satisfied: mpmath>=0.19 in ./anaconda3/lib/python3.11/si
te-packages (from sympy>=1.3->qiskit) (1.3.0)
Downloading qiskit-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_6
4.whl (5.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.6/5.6 MB 2.4 MB/s eta 0:00:00m
eta 0:00:010:01:01
Downloading rustworkx-0.14.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014
_x86_64.whl (2.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 3.3 MB/s eta 0:00:00m
eta 0:00:010:01:01
Downloading stevedore-5.2.0-py3-none-any.whl (49 kB)

file:///home/aman/Downloads/quantum computing lesson 1.html 2/10


7/23/24, 12:54 PM quantum computing lesson 1

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.7/49.7 kB 550.3 kB/s eta 0:0


0:001m1.3 MB/s eta 0:00:01
Downloading symengine-0.11.0-cp311-cp311-manylinux_2_12_x86_64.manylinux2010
_x86_64.whl (39.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 39.4/39.4 MB 2.6 MB/s eta 0:00:0
0m eta 0:00:01[36m0:00:01
Using cached pbr-6.0.0-py2.py3-none-any.whl (107 kB)
Installing collected packages: symengine, rustworkx, pbr, stevedore, qiskit
Successfully installed pbr-6.0.0 qiskit-1.0.1 rustworkx-0.14.1 stevedore-5.
2.0 symengine-0.11.0

In [19]: # Install Qiskit

!pip install qiskit-ibm-runtime

file:///home/aman/Downloads/quantum computing lesson 1.html 3/10


7/23/24, 12:54 PM quantum computing lesson 1

Collecting qiskit-ibm-runtime
Obtaining dependency information for qiskit-ibm-runtime from https://file
s.pythonhosted.org/packages/c8/b5/f7a974f2f2888a4e317c67aee5b4b8afbecbe892d2
9fcc349ac7db0edf5c/qiskit_ibm_runtime-0.20.0-py3-none-any.whl.metadata
Downloading qiskit_ibm_runtime-0.20.0-py3-none-any.whl.metadata (18 kB)
Requirement already satisfied: qiskit>=0.45.0 in ./anaconda3/lib/python3.11/
site-packages (from qiskit-ibm-runtime) (1.0.1)
Requirement already satisfied: requests>=2.19 in ./anaconda3/lib/python3.11/
site-packages (from qiskit-ibm-runtime) (2.31.0)
Collecting requests-ntlm>=1.1.0 (from qiskit-ibm-runtime)
Obtaining dependency information for requests-ntlm>=1.1.0 from https://fil
es.pythonhosted.org/packages/b6/0b/84787a85a4aee9860a510747e9a0cffd08ebfa32d
9c728b0db6306883ad1/requests_ntlm-1.2.0-py3-none-any.whl.metadata
Downloading requests_ntlm-1.2.0-py3-none-any.whl.metadata (2.4 kB)
Requirement already satisfied: numpy>=1.13 in ./anaconda3/lib/python3.11/sit
e-packages (from qiskit-ibm-runtime) (1.24.3)
Requirement already satisfied: urllib3>=1.21.1 in ./anaconda3/lib/python3.1
1/site-packages (from qiskit-ibm-runtime) (1.26.16)
Requirement already satisfied: python-dateutil>=2.8.0 in ./anaconda3/lib/pyt
hon3.11/site-packages (from qiskit-ibm-runtime) (2.8.2)
Collecting websocket-client>=1.5.1 (from qiskit-ibm-runtime)
Obtaining dependency information for websocket-client>=1.5.1 from https://
files.pythonhosted.org/packages/1e/70/1e88138a9afbed1d37093b85f0bebc3011623c
4f47c166431599fe9d6c93/websocket_client-1.7.0-py3-none-any.whl.metadata
Downloading websocket_client-1.7.0-py3-none-any.whl.metadata (7.9 kB)
Collecting ibm-platform-services>=0.22.6 (from qiskit-ibm-runtime)
Downloading ibm-platform-services-0.51.2.tar.gz (315 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 315.8/315.8 kB 1.1 MB/s eta 0:0
0:00[31m1.3 MB/s eta 0:00:01
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Collecting ibm-cloud-sdk-core<4.0.0,>=3.19.2 (from ibm-platform-services>=0.
22.6->qiskit-ibm-runtime)
Downloading ibm-cloud-sdk-core-3.19.2.tar.gz (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.0/62.0 kB 882.3 kB/s eta 0:0
0:00 eta 0:00:01
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: six>=1.5 in ./anaconda3/lib/python3.11/site-p
ackages (from python-dateutil>=2.8.0->qiskit-ibm-runtime) (1.16.0)
Requirement already satisfied: rustworkx>=0.14.0 in ./anaconda3/lib/python3.
11/site-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (0.14.1)
Requirement already satisfied: scipy>=1.5 in ./anaconda3/lib/python3.11/site
-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (1.11.1)
Requirement already satisfied: sympy>=1.3 in ./anaconda3/lib/python3.11/site
-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (1.11.1)
Requirement already satisfied: dill>=0.3 in ./anaconda3/lib/python3.11/site-
packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (0.3.6)
Requirement already satisfied: stevedore>=3.0.0 in ./anaconda3/lib/python3.1
1/site-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (5.2.0)
Requirement already satisfied: typing-extensions in ./anaconda3/lib/python3.
11/site-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (4.7.1)
Requirement already satisfied: symengine>=0.11 in ./anaconda3/lib/python3.1
1/site-packages (from qiskit>=0.45.0->qiskit-ibm-runtime) (0.11.0)

file:///home/aman/Downloads/quantum computing lesson 1.html 4/10


7/23/24, 12:54 PM quantum computing lesson 1

Requirement already satisfied: charset-normalizer<4,>=2 in ./anaconda3/lib/p


ython3.11/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2.0.4)
Requirement already satisfied: idna<4,>=2.5 in ./anaconda3/lib/python3.11/si
te-packages (from requests>=2.19->qiskit-ibm-runtime) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in ./anaconda3/lib/python
3.11/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2024.2.2)
Requirement already satisfied: cryptography>=1.3 in ./anaconda3/lib/python3.
11/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (41.0.3)
Collecting pyspnego>=0.1.6 (from requests-ntlm>=1.1.0->qiskit-ibm-runtime)
Obtaining dependency information for pyspnego>=0.1.6 from https://files.py
thonhosted.org/packages/cc/fd/06a7618de50ad13b7e85115bd1e42c1625e3365313a4c9
71898386781f89/pyspnego-0.10.2-py3-none-any.whl.metadata
Downloading pyspnego-0.10.2-py3-none-any.whl.metadata (5.4 kB)
Requirement already satisfied: cffi>=1.12 in ./anaconda3/lib/python3.11/site
-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime)
(1.15.1)
Collecting urllib3>=1.21.1 (from qiskit-ibm-runtime)
Obtaining dependency information for urllib3>=1.21.1 from https://files.py
thonhosted.org/packages/a2/73/a68704750a7679d0b6d3ad7aa8d4da8e14e151ae82e6fe
e774e6e0d05ec8/urllib3-2.2.1-py3-none-any.whl.metadata
Downloading urllib3-2.2.1-py3-none-any.whl.metadata (6.4 kB)
Collecting PyJWT<3.0.0,>=2.8.0 (from ibm-cloud-sdk-core<4.0.0,>=3.19.2->ibm-
platform-services>=0.22.6->qiskit-ibm-runtime)
Obtaining dependency information for PyJWT<3.0.0,>=2.8.0 from https://file
s.pythonhosted.org/packages/2b/4f/e04a8067c7c96c364cef7ef73906504e2f40d69081
1c021e1a1901473a19/PyJWT-2.8.0-py3-none-any.whl.metadata
Downloading PyJWT-2.8.0-py3-none-any.whl.metadata (4.2 kB)
Requirement already satisfied: pbr!=2.1.0,>=2.0.0 in ./anaconda3/lib/python
3.11/site-packages (from stevedore>=3.0.0->qiskit>=0.45.0->qiskit-ibm-runtim
e) (6.0.0)
Requirement already satisfied: mpmath>=0.19 in ./anaconda3/lib/python3.11/si
te-packages (from sympy>=1.3->qiskit>=0.45.0->qiskit-ibm-runtime) (1.3.0)
Requirement already satisfied: pycparser in ./anaconda3/lib/python3.11/site-
packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-i
bm-runtime) (2.21)
Downloading qiskit_ibm_runtime-0.20.0-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 2.3 MB/s eta 0:00:00m
eta 0:00:01[36m0:00:01
Using cached requests_ntlm-1.2.0-py3-none-any.whl (6.0 kB)
Using cached websocket_client-1.7.0-py3-none-any.whl (58 kB)
Downloading urllib3-2.2.1-py3-none-any.whl (121 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.1/121.1 kB 1.2 MB/s eta 0:0
0:00 MB/s eta 0:00:0101
Using cached pyspnego-0.10.2-py3-none-any.whl (129 kB)
Using cached PyJWT-2.8.0-py3-none-any.whl (22 kB)
Building wheels for collected packages: ibm-platform-services, ibm-cloud-sdk
-core
Building wheel for ibm-platform-services (pyproject.toml) ... done
Created wheel for ibm-platform-services: filename=ibm_platform_services-0.
51.2-py3-none-any.whl size=331363 sha256=b951eb48d8381e12f5a3e3100c8121be09e
a35d4cbf11f9e1db57c5247e5b17c
Stored in directory: /home/oem/.cache/pip/wheels/47/04/9c/02a1d4a37359e156
972bc32ed8aea2abd85a1d6e45559fe224
Building wheel for ibm-cloud-sdk-core (pyproject.toml) ... done
Created wheel for ibm-cloud-sdk-core: filename=ibm_cloud_sdk_core-3.19.2-p
y3-none-any.whl size=98827 sha256=6895674be88ef9b38961cfd8413d139db8d94bb69a

file:///home/aman/Downloads/quantum computing lesson 1.html 5/10


7/23/24, 12:54 PM quantum computing lesson 1

002e6c8fc4ddb2099e317b
Stored in directory: /home/oem/.cache/pip/wheels/82/4e/e7/3397398b197db5a5
027eb72637f96f45b6a942ed1b985e8d32
Successfully built ibm-platform-services ibm-cloud-sdk-core
Installing collected packages: websocket-client, urllib3, PyJWT, pyspnego, i
bm-cloud-sdk-core, requests-ntlm, ibm-platform-services, qiskit-ibm-runtime
Attempting uninstall: websocket-client
Found existing installation: websocket-client 0.58.0
Uninstalling websocket-client-0.58.0:
Successfully uninstalled websocket-client-0.58.0
Attempting uninstall: urllib3
Found existing installation: urllib3 1.26.16
Uninstalling urllib3-1.26.16:
Successfully uninstalled urllib3-1.26.16
Attempting uninstall: PyJWT
Found existing installation: PyJWT 2.4.0
Uninstalling PyJWT-2.4.0:
Successfully uninstalled PyJWT-2.4.0
ERROR: pip's dependency resolver does not currently take into account all th
e packages that are installed. This behaviour is the source of the following
dependency conflicts.
spyder 5.4.3 requires pyqt5<5.16, which is not installed.
spyder 5.4.3 requires pyqtwebengine<5.16, which is not installed.
conda-repo-cli 1.0.75 requires requests_mock, which is not installed.
botocore 1.29.76 requires urllib3<1.27,>=1.25.4, but you have urllib3 2.2.1
which is incompatible.
conda-repo-cli 1.0.75 requires clyent==1.2.1, but you have clyent 1.2.2 whic
h is incompatible.
conda-repo-cli 1.0.75 requires PyYAML==6.0.1, but you have pyyaml 6.0 which
is incompatible.
Successfully installed PyJWT-2.8.0 ibm-cloud-sdk-core-3.19.2 ibm-platform-se
rvices-0.51.2 pyspnego-0.10.2 qiskit-ibm-runtime-0.20.0 requests-ntlm-1.2.0
urllib3-2.2.1 websocket-client-1.7.0

Defining and displaying state vectors Qiskit's Statevector class provides functionality for
defining and manipulating quantum state vectors. The following code cell imports the
Statevector class and defines a few vectors using it. (Note that we need the sqrt function
from the NumPy library to compute the square roots for the vector u .)

In [20]: from qiskit.quantum_info import Statevector


from numpy import sqrt

u = Statevector([1 / sqrt(2), 1 / sqrt(2)])


v = Statevector([(1 + 2.0j) / 3, -2 / 3])
w = Statevector([1 / 3, 2 / 3])

print("State vectors u, v, and w have been defined.")

State vectors u, v, and w have been defined.

The Statevector class provides a draw method for displaying state vectors, including latex
and text options for different visualizations, as this code cell demonstrates:

In [21]: display(u.draw("latex"))

file:///home/aman/Downloads/quantum computing lesson 1.html 6/10


7/23/24, 12:54 PM quantum computing lesson 1

display(v.draw("text"))

√2 √2
|0⟩ + |1⟩
2 2

[ 0.33333333+0.66666667j,-0.66666667+0.j ]

The Statevector class also includes the is_valid method, which checks to see if a given
vector is a valid quantum state vector (i.e., that it has Euclidean norm equal to 1):

In [22]: display(u.is_valid())
display(w.is_valid())

True
False

Simulating measurements using Statevector Next we will see one way that
measurements of quantum states can be simulated in Qiskit, using the measure method
from the Statevector class.

First, we create a qubit state vector v and then display it.

In [23]: v = Statevector([(1 + 2.0j) / 3, -2 / 3])


v.draw("latex")

Out[23]:
1 2i 2
( + )|0⟩ − |1⟩
3 3 3

Code cells can be modified — so go ahead and change the specification of the vector if you
wish.

Next, running the measure method simulates a standard basis measurement. It returns the
result of that measurement, plus the new quantum state of our system after that
measurement.

In [154… v.measure()

Out[154… ('1',
Statevector([ 0.+0.j, -1.+0.j],
dims=(2,)))

Measurement outcomes are probabilistic, so the same method can return different results.
Try running the cell a few times to see this.

For the particular example of the vector v defined above, the measure method defines the
quantum state vector after the measurement takes place to be

(1+2j/5**0.5)∣0⟩ (rather than ∣0⟩) or −∣1⟩ rather than ∣1⟩, depending on the measurement
outcome. In both cases the alternatives are, in fact, equivalent — they are said to differ by a

file:///home/aman/Downloads/quantum computing lesson 1.html 7/10


7/23/24, 12:54 PM quantum computing lesson 1

global phase because one is equal to the other multiplied by a complex number on the unit
circle. This issue is discussed in greater detail in Lesson 3, and can safely be ignored for now.

As an aside, Statevector will throw an error if the measure method is applied to an invalid
quantum state vector. Feel free to give it a try if you're interested in seeing what an error
looks like.

Statevector also comes with a sample_counts method that allows for the simulation of
any number of measurements on the system. For example, the following cell shows the
outcome of measuring the vector v 1000 times, which (with high probability) results in the
outcome 00 approximately 5 out of every 9 times (or about 556 of the 1000 trials) and the
the outcome 1 approximately 4 out of every 9 times (or about 444 out of the 1000 trials).
The cell also demonstrates the plot_histogram function for visualizing the results.

In [155… from qiskit.visualization import plot_histogram

statistics = v.sample_counts(1000)
display(statistics)
plot_histogram(statistics)

{'0': 530, '1': 470}


Out[155…

Running the cell multiple times and trying different numbers of samples in place of 1000
may be helpful for developing some intuition for how the number of trials influences the
estimated probabilities.

file:///home/aman/Downloads/quantum computing lesson 1.html 8/10


7/23/24, 12:54 PM quantum computing lesson 1

Performing operations with Operator and *Statevector Unitary operations can be


defined and performed on state vectors in Qiskit using the *Operator class, as in the
example that follows.

In [156… from qiskit.quantum_info import Operator

X = Operator([[0, 1], [1, 0]])


Y = Operator([[0, -1.0j], [1.0j, 0]])
Z = Operator([[1, 0], [0, -1]])
H = Operator([[1 / sqrt(2), 1 / sqrt(2)], [1 / sqrt(2), -1 / sqrt(2)]])
S = Operator([[1, 0], [0, 1.0j]])
T = Operator([[1, 0], [0, (1 + 1.0j) / sqrt(2)]])

v = Statevector([1, 0])

v = v.evolve(H)
v = v.evolve(T)
v = v.evolve(H)
v = v.evolve(T)
v = v.evolve(Z)

v.draw("text")

Out[156… [ 0.85355339+0.35355339j,-0.35355339+0.14644661j]

In [157… X

Operator([[0.+0.j, 1.+0.j],
[1.+0.j, 0.+0.j]],
input_dims=(2,), output_dims=(2,))

Looking ahead toward quantum circuits Quantum circuits won't be formally introduced
until Lesson 3, but we can nevertheless experiment with composing qubit unitary
operations using Qiskit's *QuantumCircuit* class. In particular, we may define a quantum
circuit (which in this case will simply be a sequence of unitary operations performed on a
single qubit) as follows.

In [158… from qiskit import QuantumCircuit

circuit = QuantumCircuit(1)

circuit.h(0)
circuit.t(0)
circuit.h(0)
circuit.t(0)
circuit.z(0)

circuit.draw()

┌───┐┌───┐┌───┐┌───┐┌───┐
Out[158…
q: ┤ H ├┤ T ├┤ H ├┤ T ├┤ Z ├
└───┘└───┘└───┘└───┘└───┘

file:///home/aman/Downloads/quantum computing lesson 1.html 9/10


7/23/24, 12:54 PM quantum computing lesson 1

The operations are applied sequentially, starting on the left and ending on the right in the
figure. Let us first initialize a starting quantum state vector and then evolve that state
according to the sequence of operations.

In [172… ket0 = Statevector([1, 0])


v = ket0.evolve(circuit)
v.draw("text")

Out[172… [ 0.85355339+0.35355339j,-0.35355339+0.14644661j]

Finally, let's simulate the result of running this experiment (i.e., preparing the state ∣0⟩, ∣0⟩,
applying the sequence of operations represented by the circuit, and measuring) 4000
times.

In [173… statistics = v.sample_counts(4000)


plot_histogram(statistics)

Out[173…

file:///home/aman/Downloads/quantum computing lesson 1.html 10/10

You might also like