Qiskit: Difference between revisions

From wikiluntti
 
(43 intermediate revisions by the same user not shown)
Line 6: Line 6:


https://quantum-computing.ibm.com/jupyter/user/IBMQuantumChallenge2020/week-1/ex_1a_en.ipynb
https://quantum-computing.ibm.com/jupyter/user/IBMQuantumChallenge2020/week-1/ex_1a_en.ipynb
== Theory ==


=== Installation ===
=== Installation ===
Line 16: Line 14:
conda create -n qiskit python=3
conda create -n qiskit python=3
conda activate qiskit
conda activate qiskit
pip install qiskit _OR_ pip install qiskit[visualization]
pip install qiskit /////_OR_////// pip install qiskit[visualization]
</syntaxhighlight>
</syntaxhighlight>
Did not work using Python 3.9. Instead, downgrade to Python 3.8.3 in your virtual environment.
<syntaxhighlight>
conda create -n qiskit python=3
conda activate qiskit
conda install python=3.8.3
pip install qiskit ///////_OR_////// pip install qiskit[visualization]
</syntaxhighlight>
Set up the Spyder IDE https://stackoverflow.com/questions/30170468/how-to-run-spyder-in-virtual-environment#47615445


=== Setting Up Qiskit ===
=== Setting Up Qiskit ===
Line 24: Line 33:


<syntaxhighlight>
<syntaxhighlight>
from qiskit import QuantumCircuit, execute, Aer
qc = QuantumCircuit(1)  # Create a quantum circuit with one qubit
qc = QuantumCircuit(1)  # Create a quantum circuit with one qubit
initial_state = [0,1]  # Define initial_state as |1>
initial_state = [0,1]  # Define initial_state as |1>
qc.initialize(initial_state, 0) # Apply initialisation operation to the 0th qubit
qc.initialize(initial_state, 0) # Apply initialisation operation to the 0th qubit
qc.draw('text')  # Let's view our circuit (text drawing is required for the 'Initialize' gate due to a known bug in qiskit)
qc.draw('text')  # Let's view our circuit (text drawing is required for the 'Initialize' gate due to a known bug in qiskit)
backend = Aer.get_backend('statevector_simulator') # Tell Qiskit how to simulate our circuit
result = execute(qc,backend).result() # Do the simulation, returning the result
result = execute(qc,backend).result() # Do the simulation, returning the result
out_state = result.get_statevector()
out_state = result.get_statevector()
Line 34: Line 47:


<syntaxhighlight>
<syntaxhighlight>
from qiskit.visualization import plot_histogram, plot_bloch_vector
qc.measure_all()
qc.measure_all()
qc.draw()
qc.draw()
Line 55: Line 70:
Qiskit allows measuring in the Z-basis, only.
Qiskit allows measuring in the Z-basis, only.


=== Quantum Gates ===
== Theory ==
 
Identity gate.
 
Pauli X gate.
<math>\sigma_x = X = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} = |0\rangle\langle1| + |1\rangle\langle0|</math>
 
Pauli Y gate
<math>\sigma_y = Y = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} = -i|0\rangle\langle1| + i|1\rangle\langle0| </math>
 
Pauli Z gate
<math>\sigma_z = Z = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} = |0\rangle\langle0| - |1\rangle\langle1|</math>
 
[https://en.wikipedia.org/wiki/Hadamard_transform#Hadamard_gate_operations| Hadamard gate] <math>H = \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} = |+\rangle\langle0| + |-\rangle\langle1| </math>
 
R gate <math>R_\phi = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\phi} \end{bmatrix}</math>
 
S gate or <math>\sqrt Z</math> gate <math>S = \begin{bmatrix} 1 & 0 \\ 0 & e^{\frac{i\pi}{2}} \end{bmatrix}</math>
 
T gate <math>T = \begin{bmatrix} 1 & 0 \\ 0 & e^{\frac{i\pi}{4}} \end{bmatrix}</math>
 
U1 gate: <math>
U_1 = U_3(0, 0, \lambda) = U_1 = \begin{bmatrix} 1 & 0 \\
            0 & e^{i\lambda}\\
    \end{bmatrix}
</math>


U2 gate: <math>
Quantum operations are reversible, thus the reversible computing. That makes some ''complications'' to the gate design.
U_2 = U_3(\tfrac{\pi}{2}, \phi, \lambda) = \tfrac{1}{\sqrt{2}}\begin{bmatrix} 1 & -e^{i\lambda} \\
            e^{i\phi} & e^{i\lambda+i\phi}
    \end{bmatrix}
</math>
 
<syntaxhighlight>
qc = QuantumCircuit(1)
qc.x(0)
#qc.y(0) # Y-gate on qubit 0
#qc.z(0) # Z-gate on qubit 0
#qc.rz(pi/4, 0)
#qc.s(0)  # Apply S-gate to qubit 0
#qc.sdg(0) # Apply Sdg-gate to qubit 0
qc.t(0)  # Apply T-gate to qubit 0
qc.tdg(0) # Apply Tdg-gate to qubit 0


qc.draw('mpl')
[https://www.cod3v.info/index.php?title=Quantum_Gates Quantum Gates ]
# Let's see the result
backend = Aer.get_backend('statevector_simulator')
out = execute(qc,backend).result().get_statevector()
plot_bloch_multivector(out)
</syntaxhighlight>


Clifford Gates


=== Multiple Qubits ===
[[Grover's Algorithm]]


Eg. <math>X\otimes H = \begin{bmatrix} 0 &H\\H&0\end{bmatrix}</math>.
[[qRAM]]
<syntaxhighlight>
backend = Aer.get_backend('unitary_simulator')
unitary = execute(qc,backend).result().get_unitary()
#
# In Jupyter Notebooks we can display this nicely using Latex.
# If not using Jupyter Notebooks you may need to remove the
# array_to_latex function and use print(unitary) instead.
from qiskit_textbook.tools import array_to_latex
array_to_latex(unitary, pretext="\\text{Circuit = }\n")
</syntaxhighlight>
 
Eg. CNOT is a conditional gate that performs an X-gate on the second qubit, if the state of the first qubit (control) is  <math>|1\rangle</math>. <math>CNOT= \begin{bmatrix} 1 & 0 & 0 & 0 \\
                              0 & 0 & 0 & 1 \\
                              0 & 0 & 1 & 0 \\
                              0 & 1 & 0 & 0 \\
              \end{bmatrix}
</math>. This matrix swaps the amplitudes of  |01⟩  and  |11⟩  in the statevector.
 
CNOT if a control qubit is on the superposition: <math>\text{CNOT}|0{+}\rangle = \tfrac{1}{\sqrt{2}}(|00\rangle + |11\rangle)</math>, which is ''Bell State''. Entanglement, but [https://arxiv.org/abs/quant-ph/0212023 no-communication theorem].
 
 
<syntaxhighlight>
qc = QuantumCircuit(2)
# Apply H-gate to the first:
qc.h(0)
# Apply a CNOT:
qc.cx(0,1)
qc.draw()
#
# Let's see the result:
backend = Aer.get_backend('statevector_simulator')
final_state = execute(qc,backend).result().get_statevector()
# Print the statevector neatly:
array_to_latex(final_state, pretext="\\text{Statevector = }")
#
results = execute(qc,backend).result().get_counts()
plot_histogram(results)
</syntaxhighlight>
 
Entanglement.
 
 
<syntaxhighlight>
</syntaxhighlight>
 
<syntaxhighlight>
</syntaxhighlight>


== Exercises ==
== Exercises ==

Latest revision as of 16:16, 27 November 2020

Introduction

https://quantum-computing.ibm.com/

https://quantum-computing.ibm.com/challenges/fall-2020

https://quantum-computing.ibm.com/jupyter/user/IBMQuantumChallenge2020/week-1/ex_1a_en.ipynb

Installation

Installation https://qiskit.org/documentation/install.html

conda create -n qiskit python=3
conda activate qiskit
pip install qiskit /////_OR_////// pip install qiskit[visualization]

Did not work using Python 3.9. Instead, downgrade to Python 3.8.3 in your virtual environment.

conda create -n qiskit python=3
conda activate qiskit
conda install python=3.8.3
pip install qiskit ///////_OR_////// pip install qiskit[visualization]

Set up the Spyder IDE https://stackoverflow.com/questions/30170468/how-to-run-spyder-in-virtual-environment#47615445

Setting Up Qiskit

https://qiskit.org/textbook/ch-states/representing-qubit-states.html

from qiskit import QuantumCircuit, execute, Aer

qc = QuantumCircuit(1)  # Create a quantum circuit with one qubit
initial_state = [0,1]   # Define initial_state as |1>
qc.initialize(initial_state, 0) # Apply initialisation operation to the 0th qubit
qc.draw('text')  # Let's view our circuit (text drawing is required for the 'Initialize' gate due to a known bug in qiskit)

backend = Aer.get_backend('statevector_simulator') # Tell Qiskit how to simulate our circuit
result = execute(qc,backend).result() # Do the simulation, returning the result
out_state = result.get_statevector()
print(out_state) # Display the output state vector
from qiskit.visualization import plot_histogram, plot_bloch_vector

qc.measure_all()
qc.draw()
result = execute(qc,backend).result()
counts = result.get_counts()
plot_histogram(counts)

Take superposition as initial state

initial_state = [1/sqrt(2), 1j/sqrt(2)]  # Define state |q>

The Bloch Sphere

from qiskit_textbook.widgets import plot_bloch_vector_spherical
coords = [pi/2,0,1] # [Theta, Phi, Radius]
plot_bloch_vector_spherical(coords) # Bloch Vector with spherical coordinates

Qiskit allows measuring in the Z-basis, only.

Theory

Quantum operations are reversible, thus the reversible computing. That makes some complications to the gate design.

Quantum Gates

Clifford Gates

Grover's Algorithm

qRAM

Exercises

Week 1

Week 2

Week 3