# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="Tc_OTCECOUtf" colab_type="code" colab={}
# !pip install qiskit

from qiskit import *
from qiskit.visualization import plot_histogram

# + id="yQ02iAhMhenC" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 62} outputId="37dc6134-39c1-48db-9a13-9f33622a4ef6"
from math import sqrt, pi


#Initialization states must be normalized, otherwise you may recieve error

#Let's try 1 & 0

initial_state = [1,0]
qc = QuantumCircuit(1)
qc.initialize(initial_state,0)
qc.draw()

# + id="1cn0GTkRicp-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="a1e2ac52-e1be-469d-9610-9f0082f20e4c"
get_states = execute(qc, Aer.get_backend('statevector_simulator')).result()
print(get_states.get_statevector())

# + id="IuSQVK82i1Yg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 323} outputId="133531ac-1e38-4798-805e-95a594556e3f"
plot_histogram(get_states.get_counts())

# + [markdown] id="eca06cnLi_qH" colab_type="text"
# # Let's try Superposition

# + id="SZSbnD0ljC7I" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 62} outputId="4c05801e-df37-4821-aaa5-061ad2c6f6fc"
qc_1 = QuantumCircuit(1)
superposition_state = [1/sqrt(2), 1/sqrt(2)]
qc_1.initialize(superposition_state,0)
qc_1.draw()

# + id="HzQ25aaOjXbY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="1d805bd7-1169-4b48-c449-474e079958ee"
get_superposition_states = execute(qc_1, Aer.get_backend("statevector_simulator")).result()
print(get_superposition_states.get_statevector())

# + id="yZpjO-1jjnw8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 328} outputId="1d4b2a4c-2599-409d-c57e-2bba5303099d"
plot_histogram(get_superposition_states.get_counts())

# + [markdown] id="azD33XgcjzNQ" colab_type="text"
# # Rules of Measurement (Normalization Exercise)

# + [markdown] id="MbX5J9O_j7TI" colab_type="text"
# Create a state vector that will give a  1/3  probability of measuring  |0⟩ .

# + id="uwFrebg9j2JB" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 328} outputId="e884a053-8781-42f6-ea4f-d12092ffd405"
state_vector = [1/sqrt(3), sqrt(2)/sqrt(3)]

ex =  QuantumCircuit(1)
ex.initialize(state_vector,0)

backend = Aer.get_backend('statevector_simulator')

plot_histogram(execute(ex,backend).result().get_counts())

# + [markdown] id="XZ5Na_Y1lS7p" colab_type="text"
# Create a different state vector that will give the same measurement probabilities.

# + [markdown] id="JjGCOuFxnJpf" colab_type="text"
# **In the equation above,  |x⟩  can be any qubit state. To find the probability of measuring  |x⟩ , we take the inner product of  |x⟩  and the state we are measuring (in this case  |ψ⟩ ), then square the magnitude. This may seem a little convoluted, but it will soon become second nature.**

# + [markdown] id="o5eImSQ1neyX" colab_type="text"
# ![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMoAAAAwCAYAAABQbOXXAAAJ10lEQVR4Ae1crde6PBjef7FIJBqJRKKRSDQa/ROIRKPRSDQaiUQj0UgkXu/ZYDgnbFPxcf7ePec8B+Rj3LvvXfc3EPg/zwHPASMHiPEKf4HngMMcaMot1nGEgFCEaYGq/QyxHiif4asf9Q840J42CNIjGvas5oiUEhCSofwAWDxQ/kCg/hGf4ECNYrVCXt/Gbg4JCCEI5YO302/teaC8xT5/89c4cC2REgJCdzh3AxV1gRU7Fu1xWZgwD5SFGeqH+yMOdDXyiIEiRyWAcj1izYBCdqgWJsMBoDQ<KEY>)

# + [markdown] id="tjj9i-QLnhCv" colab_type="text"
# Probability of measuring |x> state for any other state is basically dot product of <x| (column vector) with the current state and then, squaring the magnitude

# + [markdown] id="2yrKrR8ip-16" colab_type="text"
# # Collapsing of Qubits post-measurement (Checking with Superposition)

# + id="mfTvNz6NnyAh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 323} outputId="8610bbc1-974e-4865-dc79-0ba09bbd6fc5"
collapse = QuantumCircuit(1)

collapse.initialize(superposition_state,0)

collapse.measure_all()

plot_histogram(execute(collapse,backend).result().get_counts())

# + [markdown] id="u7101Pnuqfwg" colab_type="text"
# Yes. The qubit was initialised with superposition state-vectors, however post-measuring it, the state was collapsed - as no probabilities of 1 was found.

# + id="PK9fD8iClTwQ" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered