Project I Merged

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

8/17/23, 8:55 PM project I

In [1]: import numpy as np


from qiskit import *

In [2]: circ = QuantumCircuit(3)

In [3]: # Add a H gate on qubit $q_{0}$, putting this qubit in superposition.


circ.h(0)
# Add a CX (CNOT) gate on control qubit $q_{0}$ and target qubit $q_{1}$, putting
# the qubits in a Bell state.
circ.cx(0, 1)
# Add a CX (CNOT) gate on control qubit $q_{0}$ and target qubit $q_{2}$, putting
# the qubits in a GHZ state.
circ.cx(0, 2)

<qiskit.circuit.instructionset.InstructionSet at 0x265ecdc1b70>
Out[3]:

In [4]: circ.draw('mpl')

Out[4]:

In [9]: !pip install qiskit-aer

localhost:8888/nbconvert/html/project I.ipynb?download=false 1/6


8/17/23, 8:55 PM project I
Collecting qiskit-aer
Obtaining dependency information for qiskit-aer from https://files.pythonhosted.or
g/packages/10/3a/09935130dbb72ec4154a067f5611c83ae25eb1af1ff9ffb5e445b33afb33/qiskit_
aer-0.12.2-cp311-cp311-win_amd64.whl.metadata
Downloading qiskit_aer-0.12.2-cp311-cp311-win_amd64.whl.metadata (6.5 kB)
Requirement already satisfied: qiskit-terra>=0.21.0 in c:\users\marij\appdata\roaming
\python\python311\site-packages (from qiskit-aer) (0.25.0)
Requirement already satisfied: numpy>=1.16.3 in c:\users\marij\appdata\roaming\python
\python311\site-packages (from qiskit-aer) (1.25.2)
Requirement already satisfied: scipy>=1.0 in c:\users\marij\appdata\roaming\python\py
thon311\site-packages (from qiskit-aer) (1.11.1)
Requirement already satisfied: rustworkx>=0.13.0 in c:\users\marij\appdata\roaming\py
thon\python311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (0.13.1)
Requirement already satisfied: ply>=3.10 in c:\users\marij\appdata\roaming\python\pyt
hon311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (3.11)
Requirement already satisfied: psutil>=5 in c:\users\marij\appdata\roaming\python\pyt
hon311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (5.9.5)
Requirement already satisfied: sympy>=1.3 in c:\users\marij\appdata\roaming\python\py
thon311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (1.12)
Requirement already satisfied: dill>=0.3 in c:\users\marij\appdata\roaming\python\pyt
hon311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (0.3.7)
Requirement already satisfied: python-dateutil>=2.8.0 in c:\users\marij\appdata\roami
ng\python\python311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (2.8.2)
Requirement already satisfied: stevedore>=3.0.0 in c:\users\marij\appdata\roaming\pyt
hon\python311\site-packages (from qiskit-terra>=0.21.0->qiskit-aer) (5.1.0)
Requirement already satisfied: six>=1.5 in c:\users\marij\appdata\roaming\python\pyth
on311\site-packages (from python-dateutil>=2.8.0->qiskit-terra>=0.21.0->qiskit-aer)
(1.16.0)
Requirement already satisfied: pbr!=2.1.0,>=2.0.0 in c:\users\marij\appdata\roaming\p
ython\python311\site-packages (from stevedore>=3.0.0->qiskit-terra>=0.21.0->qiskit-ae
r) (5.11.1)
Requirement already satisfied: mpmath>=0.19 in c:\users\marij\appdata\roaming\python
\python311\site-packages (from sympy>=1.3->qiskit-terra>=0.21.0->qiskit-aer) (1.3.0)
Downloading qiskit_aer-0.12.2-cp311-cp311-win_amd64.whl (9.6 MB)
---------------------------------------- 0.0/9.6 MB ? eta -:--:--
---------------------------------------- 0.0/9.6 MB 1.3 MB/s eta 0:00:08
---------------------------------------- 0.1/9.6 MB 1.3 MB/s eta 0:00:08
- -------------------------------------- 0.3/9.6 MB 2.0 MB/s eta 0:00:05
- -------------------------------------- 0.3/9.6 MB 2.0 MB/s eta 0:00:05
- -------------------------------------- 0.3/9.6 MB 1.4 MB/s eta 0:00:07
-- ------------------------------------- 0.5/9.6 MB 2.1 MB/s eta 0:00:05
-- ------------------------------------- 0.6/9.6 MB 2.2 MB/s eta 0:00:05
--- ------------------------------------ 0.9/9.6 MB 2.5 MB/s eta 0:00:04
---- ----------------------------------- 1.1/9.6 MB 2.7 MB/s eta 0:00:04
----- ---------------------------------- 1.3/9.6 MB 3.0 MB/s eta 0:00:03
----- ---------------------------------- 1.4/9.6 MB 2.8 MB/s eta 0:00:03
------ --------------------------------- 1.7/9.6 MB 2.9 MB/s eta 0:00:03
-------- ------------------------------- 2.0/9.6 MB 3.2 MB/s eta 0:00:03
-------- ------------------------------- 2.1/9.6 MB 3.2 MB/s eta 0:00:03
--------- ------------------------------ 2.3/9.6 MB 3.3 MB/s eta 0:00:03
---------- ----------------------------- 2.5/9.6 MB 3.5 MB/s eta 0:00:03
----------- ---------------------------- 2.8/9.6 MB 3.6 MB/s eta 0:00:02
------------ --------------------------- 2.9/9.6 MB 3.6 MB/s eta 0:00:02
------------- -------------------------- 3.2/9.6 MB 3.7 MB/s eta 0:00:02
-------------- ------------------------- 3.4/9.6 MB 3.8 MB/s eta 0:00:02
--------------- ------------------------ 3.7/9.6 MB 3.9 MB/s eta 0:00:02
--------------- ------------------------ 3.8/9.6 MB 3.9 MB/s eta 0:00:02
---------------- ----------------------- 3.9/9.6 MB 3.8 MB/s eta 0:00:02
---------------- ----------------------- 3.9/9.6 MB 3.5 MB/s eta 0:00:02
---------------- ----------------------- 3.9/9.6 MB 3.6 MB/s eta 0:00:02
localhost:8888/nbconvert/html/project I.ipynb?download=false 2/6
8/17/23, 8:55 PM project I

---------------- ----------------------- 3.9/9.6 MB 3.6 MB/s eta 0:00:02


------------------ --------------------- 4.5/9.6 MB 3.7 MB/s eta 0:00:02
------------------- -------------------- 4.6/9.6 MB 3.7 MB/s eta 0:00:02
------------------- -------------------- 4.8/9.6 MB 3.6 MB/s eta 0:00:02
-------------------- ------------------- 5.0/9.6 MB 3.7 MB/s eta 0:00:02
---------------------- ----------------- 5.3/9.6 MB 3.8 MB/s eta 0:00:02
----------------------- ---------------- 5.6/9.6 MB 3.8 MB/s eta 0:00:02
------------------------ --------------- 5.8/9.6 MB 3.9 MB/s eta 0:00:01
------------------------- -------------- 6.1/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------- ------------- 6.4/9.6 MB 4.0 MB/s eta 0:00:01
--------------------------- ------------ 6.5/9.6 MB 4.0 MB/s eta 0:00:01
--------------------------- ------------ 6.7/9.6 MB 4.0 MB/s eta 0:00:01
---------------------------- ----------- 6.9/9.6 MB 4.0 MB/s eta 0:00:01
----------------------------- ---------- 7.1/9.6 MB 4.0 MB/s eta 0:00:01
----------------------------- ---------- 7.1/9.6 MB 4.0 MB/s eta 0:00:01
------------------------------ --------- 7.4/9.6 MB 4.0 MB/s eta 0:00:01
------------------------------- -------- 7.5/9.6 MB 3.9 MB/s eta 0:00:01
------------------------------- -------- 7.6/9.6 MB 4.0 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
-------------------------------- ------- 7.9/9.6 MB 3.9 MB/s eta 0:00:01
--------------------------------- ------ 8.1/9.6 MB 3.5 MB/s eta 0:00:01
--------------------------------- ------ 8.1/9.6 MB 3.5 MB/s eta 0:00:01
--------------------------------- ------ 8.1/9.6 MB 3.5 MB/s eta 0:00:01
---------------------------------- ----- 8.2/9.6 MB 3.3 MB/s eta 0:00:01
----------------------------------- ---- 8.4/9.6 MB 3.4 MB/s eta 0:00:01
----------------------------------- ---- 8.4/9.6 MB 3.4 MB/s eta 0:00:01
----------------------------------- ---- 8.4/9.6 MB 3.4 MB/s eta 0:00:01
------------------------------------ --- 8.7/9.6 MB 3.3 MB/s eta 0:00:01
------------------------------------ --- 8.7/9.6 MB 3.3 MB/s eta 0:00:01
------------------------------------ --- 8.9/9.6 MB 3.3 MB/s eta 0:00:01
------------------------------------- -- 9.0/9.6 MB 3.3 MB/s eta 0:00:01
------------------------------------- -- 9.0/9.6 MB 3.2 MB/s eta 0:00:01
------------------------------------- -- 9.0/9.6 MB 3.2 MB/s eta 0:00:01
------------------------------------- -- 9.0/9.6 MB 3.2 MB/s eta 0:00:01
------------------------------------- -- 9.1/9.6 MB 3.1 MB/s eta 0:00:01
-------------------------------------- - 9.2/9.6 MB 3.1 MB/s eta 0:00:01
-------------------------------------- - 9.3/9.6 MB 3.1 MB/s eta 0:00:01
-------------------------------------- - 9.3/9.6 MB 3.1 MB/s eta 0:00:01
-------------------------------------- - 9.4/9.6 MB 3.0 MB/s eta 0:00:01
-------------------------------------- - 9.4/9.6 MB 3.0 MB/s eta 0:00:01
--------------------------------------- 9.4/9.6 MB 3.0 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.9 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.7 MB/s eta 0:00:01
--------------------------------------- 9.5/9.6 MB 2.7 MB/s eta 0:00:01
--------------------------------------- 9.6/9.6 MB 2.6 MB/s eta 0:00:01
--------------------------------------- 9.6/9.6 MB 2.6 MB/s eta 0:00:01
--------------------------------------- 9.6/9.6 MB 2.6 MB/s eta 0:00:01
--------------------------------------- 9.6/9.6 MB 2.5 MB/s eta 0:00:01
--------------------------------------- 9.6/9.6 MB 2.5 MB/s eta 0:00:01
localhost:8888/nbconvert/html/project I.ipynb?download=false 3/6
8/17/23, 8:55 PM project I
---------------------------------------- 9.6/9.6 MB 2.4 MB/s eta 0:00:00
Installing collected packages: qiskit-aer
Successfully installed qiskit-aer-0.12.2

In [10]: # Import Aer


from qiskit import Aer

# Run the quantum circuit on a statevector simulator backend


backend = Aer.get_backend('statevector_simulator')

In [11]: job = backend.run(circ)

In [12]: result = job.result()

In [13]: outputstate = result.get_statevector(circ, decimals=3)


print(outputstate)

Statevector([0.707+0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,


0. +0.j, 0. +0.j, 0.707+0.j],
dims=(2, 2, 2))

In [14]: from qiskit.visualization import plot_state_city


plot_state_city(outputstate)

Out[14]:

In [15]: # Run the quantum circuit on a unitary simulator backend


backend = Aer.get_backend('unitary_simulator')
job = backend.run(circ)
result = job.result()

# Show the results


print(result.get_unitary(circ, decimals=3))

localhost:8888/nbconvert/html/project I.ipynb?download=false 4/6


8/17/23, 8:55 PM project I
Operator([[ 0.707+0.j, 0.707-0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0.707+0.j, -0.707+0.j],
[ 0. +0.j, 0. +0.j, 0.707+0.j, 0.707-0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0.707+0.j,
-0.707+0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0.707+0.j,
0.707-0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0.707+0.j, -0.707+0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j],
[ 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0.707+0.j, 0.707-0.j],
[ 0.707+0.j, -0.707+0.j, 0. +0.j, 0. +0.j, 0. +0.j,
0. +0.j, 0. +0.j, 0. +0.j]],
input_dims=(2, 2, 2), output_dims=(2, 2, 2))

In [16]: # Create a Quantum Circuit


meas = QuantumCircuit(3, 3)
meas.barrier(range(3))
# map the quantum measurement to the classical bits
meas.measure(range(3), range(3))

# The Qiskit circuit object supports composition using


# the compose method.
circ.add_register(meas.cregs[0])
qc = circ.compose(meas)

#drawing the circuit


qc.draw()

┌───┐ ░ ┌─┐
Out[16]:
q_0: ┤ H ├──■────■───░─┤M├──────
└───┘┌─┴─┐ │ ░ └╥┘┌─┐
q_1: ─────┤ X ├──┼───░──╫─┤M├───
└───┘┌─┴─┐ ░ ║ └╥┘┌─┐
q_2: ──────────┤ X ├─░──╫──╫─┤M├
└───┘ ░ ║ ║ └╥┘
c: 3/═══════════════════╩══╩══╩═
0 1 2

In [17]: # Use Aer's qasm_simulator


backend_sim = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.


# We've set the number of repeats of the circuit
# to be 1024, which is the default.
job_sim = backend_sim.run(transpile(qc, backend_sim), shots=1024)

# Grab the results from the job.


result_sim = job_sim.result()

In [18]: counts = result_sim.get_counts(qc)


print(counts)

{'111': 496, '000': 528}

In [19]: from qiskit.visualization import plot_histogram


plot_histogram(counts)

localhost:8888/nbconvert/html/project I.ipynb?download=false 5/6


8/17/23, 8:55 PM project I

Out[19]:

In [ ]:

localhost:8888/nbconvert/html/project I.ipynb?download=false 6/6


8/18/23, 12:21 AM Untitled2

In [1]: from qiskit import QuantumCircuit, transpile, assemble, Aer, execute


from qiskit.visualization import plot_bloch_multivector, plot_histogram

In [2]: # Create a quantum circuit with 2 qubits


qc = QuantumCircuit(2)

In [3]: # Apply Hadamard gates to both qubits


qc.h(0)
qc.h(1)

<qiskit.circuit.instructionset.InstructionSet at 0x1f176e651e0>
Out[3]:

In [4]: # Visualize the circuit


print(qc)

┌───┐
q_0: ┤ H ├
├───┤
q_1: ┤ H ├
└───┘

In [5]: # Simulate the circuit using the Aer simulator


simulator = Aer.get_backend('statevector_simulator')
job = execute(qc, simulator)
result = job.result()
statevector = result.get_statevector()

In [6]: # Plot the Bloch sphere representation of the statevector


plot_bloch_multivector(statevector)

Out[6]:

In [11]: simulator = Aer.get_backend('qasm_simulator')

In [12]: # Transpile the circuit for the simulator


compiled_circuit = transpile(qc, simulator)

localhost:8888/nbconvert/html/Untitled2.ipynb?download=false 1/2
8/18/23, 12:21 AM Untitled2

In [14]: #Run the transpiled circuit on the simulator


job = simulator.run(compiled_circuit, shots=1024) # Adjust shots as needed
result = job.result()
counts = result.get_counts()

In [15]: # Plot a histogram of the measurement results


plot_histogram(counts)

Out[15]:

In [ ]:

localhost:8888/nbconvert/html/Untitled2.ipynb?download=false 2/2
8/18/23, 12:20 AM Untitled3

In [4]: from qiskit import QuantumCircuit, Aer, transpile, assemble


from qiskit.visualization import plot_histogram
from qiskit.visualization import circuit_drawer

In [2]: # Create a quantum circuit with 2 qubits


qc = QuantumCircuit(2, 2)

In [3]: # Apply a Hadamard gate to the first qubit


qc.h(0)

<qiskit.circuit.instructionset.InstructionSet at 0x1f165561d80>
Out[3]:

In [10]: # Apply a controlled rotation gate to the second qubit


theta = 0.5

In [12]: qc.crx(theta, 0, 1)

<qiskit.circuit.instructionset.InstructionSet at 0x1f17fbc6ad0>
Out[12]:

In [13]: # Measure both qubits


qc.measure([0, 1], [0, 1])

<qiskit.circuit.instructionset.InstructionSet at 0x1f17fbc7490>
Out[13]:

In [15]: simulator = Aer.get_backend('qasm_simulator')

In [16]: # Transpile the circuit for the simulator


compiled_circuit = transpile(qc, simulator)

In [17]: # Run the transpiled circuit on the simulator


job = simulator.run(compiled_circuit, shots=1024)

In [18]: # Adjust shots as needed


result = job.result()

In [19]: counts = result.get_counts()

In [20]: print("Measurement outcomes:", counts)

Measurement outcomes: {'00': 525, '01': 464, '11': 35}

In [23]: plot_histogram(counts)

localhost:8888/nbconvert/html/Untitled3.ipynb?download=false 1/2
8/18/23, 12:20 AM Untitled3

Out[23]:

In [ ]:

localhost:8888/nbconvert/html/Untitled3.ipynb?download=false 2/2
8/18/23, 12:20 AM Untitled4

In [2]: from qiskit import QuantumCircuit, Aer, transpile, assemble


from qiskit.visualization import plot_histogram
from qiskit.visualization import circuit_drawer , save

In [3]: qc = QuantumCircuit(2, 2)

In [4]: qc.h(0)

<qiskit.circuit.instructionset.InstructionSet at 0x1d78a8c7f10>
Out[4]:

In [5]: theta = 0.5

In [6]: qc.crx(theta, 0, 1)

<qiskit.circuit.instructionset.InstructionSet at 0x1d79e2d4c10>
Out[6]:

In [8]: qc.cry(theta, 0, 1)

<qiskit.circuit.instructionset.InstructionSet at 0x1d7a0265630>
Out[8]:

In [10]: circuit_drawer(qc, output='mpl', filename='circuit.png')

Out[10]:

In [11]: import matplotlib


matplotlib.use('TkAgg')

In [12]: # Use the TkAgg backend

from qiskit.visualization import circuit_drawer

In [15]: from qiskit import QuantumCircuit, Aer, transpile


from qiskit.visualization import circuit_drawer

# Create a quantum circuit with 3 qubits


qc = QuantumCircuit(3)

# Add Hadamard gate to qubit 0


qc.h(0)

theta = 0.5

localhost:8888/nbconvert/html/Untitled4.ipynb?download=false 1/2
8/18/23, 12:20 AM Untitled4

# Add a controlled X gate (CNOT) from qubit 0 to qubit 1


qc.crx(theta , 0, 1)

# Add a controlled Y gate from qubit 0 to qubit 2


qc.cry(theta, 0, 2)

# Draw the quantum circuit


circuit_drawer(qc, output='mpl').show()

# Simulate the circuit


simulator = Aer.get_backend('statevector_simulator')
compiled_circuit = transpile(qc, simulator)
job = simulator.run(compiled_circuit)
result = job.result()
statevector = result.get_statevector()

print("Final statevector:", statevector)

Final statevector: Statevector([ 7.07106781e-01-1.34602383e-18j,


6.63825681e-01+1.30417921e-18j,
1.07120478e-17+4.52180677e-19j,
-1.03790362e-17-1.69502525e-01j,
7.75970334e-18+2.40395653e-35j,
1.69502525e-01+3.33011626e-19j,
-5.41306986e-35+7.18719516e-36j,
-2.65020305e-18-4.32811003e-02j],
dims=(2, 2, 2))

In [ ]:

localhost:8888/nbconvert/html/Untitled4.ipynb?download=false 2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy