Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/notebooks/ch-labs/Lab02_Single_Qubit_Gates.ipynb
3855 views
Kernel: Python 3

Lab 2 Single Qubit Gates

Prerequisite Ch.1.3 Representing Qubit States Ch.1.4 Single Qubit Gates

Other relevant materials Grokking the Bloch Sphere

import numpy as np # Importing standard Qiskit libraries from qiskit import QuantumCircuit, transpile, Aer, IBMQ, execute from qiskit.tools.jupyter import * from qiskit.visualization import * from ibm_quantum_widgets import * from qiskit.providers.aer import QasmSimulator # Loading your IBM Quantum account(s) provider = IBMQ.load_account() backend = Aer.get_backend('statevector_simulator')

Part 1 - Effect of Single-Qubit Gates on state |0>


Goal

Create quantum circuits to apply various single qubit gates on state |0> and understand the change in state and phase of the qubit.

X gate applied to 0 stateY gate applied to 0 stateZ gate applied to 0 stateS gate applied to 0 state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc1 = QuantumCircuit(4) # perform gate operations on individual qubits qc1.x(0) qc1.y(1) qc1.z(2) qc1.s(3) # Draw circuit qc1.draw()
Image in a Jupyter notebook
# Plot blochshere out1 = execute(qc1,backend).result().get_statevector() plot_bloch_multivector(out1)
/opt/conda/lib/python3.8/site-packages/qiskit/visualization/bloch.py:69: MatplotlibDeprecationWarning: The M attribute was deprecated in Matplotlib 3.4 and will be removed two minor releases later. Use self.axes.M instead. x_s, y_s, _ = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
Image in a Jupyter notebook
Effect on Qubit on application of GateSatevectorQSphere PlotStatevector (Post Measurement)
Input State = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}

Before measurement,
- qubit state = (0+0j1+0j)\begin{pmatrix}0+0j & 1+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘1’

Post measurement, quit state is ‘1’ with phase 0
altaltalt
Input State = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}

Before measurement,
- qubit state = (0+0j0+1j)\begin{pmatrix}0+0j & 0+1j\end{pmatrix}
- qubit has probability 1 of being in state ‘1’

Post measurement, quit state is ‘1’ with phase pi/2
altaltalt
Input State = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}

Before measurement,
- qubit state = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘0’

Post measurement, quit state is ‘0’ with phase 0
altaltalt
Input State = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}

Before measurement,
- qubit state = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘0’

Post measurement, quit state is ‘0’ with phase 0
altaltalt

Part 2 - Effect of Single-Qubit Gates on state |1>


Goal

Create quantum circuits to apply various single qubit gates on state |1> and understand the change in state and phase of the qubit.

X gate applied to 1 stateY gate applied to 1 stateZ gate applied to 1 stateS gate applied to 1 state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc2 = QuantumCircuit(4) # initialize qubits qc2.x(range(4)) # perform gate operations on individual qubits qc2.x(0) qc2.y(1) qc2.z(2) qc2.s(3) # Draw circuit qc2.draw()
# Plot blochshere out2 = execute(qc2,backend).result().get_statevector() plot_bloch_multivector(out2)
Effect on Qubit on application of GateSatevectorQSphere PlotStatevector (Post Measurement)
Input State = (0+0j1+0j)\begin{pmatrix}0+0j & 1+0j\end{pmatrix}

Before measurement,
- qubit state = (1+0j0+0j)\begin{pmatrix}1+0j & 0+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘0’

Post measurement, quit state is ‘0’ with phase 0
altaltalt
Input State = (0+0j1+0j)\begin{pmatrix}0+0j & 1+0j\end{pmatrix}

Before measurement,
- qubit state = (01j0+0j)\begin{pmatrix}0-1j & 0+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘0’

Post measurement, quit state is ‘0’ with phase 3pi/2
altaltalt
Input State = (0+0j1+0j)\begin{pmatrix}0+0j & 1+0j\end{pmatrix}

Before measurement,
- qubit state = (0+0j1+0j)\begin{pmatrix}0+0j & -1+0j\end{pmatrix}
- qubit has probability 1 of being in state ‘1’

Post measurement, quit state is ‘1’ with phase pi
altaltalt
Input State = (0+0j1+0j)\begin{pmatrix}0+0j & 1+0j\end{pmatrix}

Before measurement,
- qubit state = (0+0j0+1j)\begin{pmatrix}0+0j & 0+1j\end{pmatrix}
- qubit has probability 1 of being in state ‘1’

Post measurement, quit state is ‘1’ with phase pi/2
altaltalt

Part 3 - Effect of Single-Qubit Gates on state |+>


Goal

Create quantum circuits to apply various single qubit gates on state |+> and understand the change in state and phase of the qubit.

X gate applied to + stateY gate applied to + stateZ gate applied to + stateS gate applied to + state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc3 = QuantumCircuit(4) # initialize qubits qc3.h(range(4)) # perform gate operations on individual qubits qc3.x(0) qc3.y(1) qc3.z(2) qc3.s(3) # Draw circuit qc3.draw()
# Plot blochshere out3 = execute(qc3,backend).result().get_statevector() plot_bloch_multivector(out3)
Effect on Qubit on application of GateSatevectorQSphere PlotProbability (Histogram)
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (00.707j0+0.707j)\begin{pmatrix}0-0.707j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt

Part 4 - Effect of Single-Qubit Gates on state |->


Goal

Create quantum circuits to apply various single qubit gates on state |-> and understand the change in state and phase of the qubit.

X gate applied to - stateY gate applied to - stateZ gate applied to - stateS gate applied to - state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc4 = QuantumCircuit(4) # initialize qubits qc4.x(range(4)) qc4.h(range(4)) # perform gate operations on individual qubits qc4.x(0) qc4.y(1) qc4.z(2) qc4.s(3) # Draw circuit qc4.draw()
# Plot blochshere out4 = execute(qc4,backend).result().get_statevector() plot_bloch_multivector(out4)
Effect on Qubit on application of GateSatevectorQSphere PlotProbability (Histogram)
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}-0.707+0j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0+0.707j0+0.707j)\begin{pmatrix}0+0.707j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt

Part 5 - Effect of Single-Qubit Gates on state |i>


Goal

Create quantum circuits to apply various single qubit gates on state |i> and understand the change in state and phase of the qubit.

X gate applied to i stateY gate applied to i stateZ gate applied to i stateS gate applied to i state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc5 = QuantumCircuit(4) # initialize qubits qc5.h(range(4)) qc5.s(range(4)) # perform gate operations on individual qubits qc5.x(0) qc5.y(1) qc5.z(2) qc5.s(3) # Draw circuit qc5.draw()
# Plot blochshere out5 = execute(qc5,backend).result().get_statevector() plot_bloch_multivector(out5)
Effect on Qubit on application of GateSatevectorQSphere PlotProbability (Histogram)
Input State = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}

Before measurement,
- qubit state = (0+0.707j0.707+0j)\begin{pmatrix}0+0.707j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & -0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt

Part 6 - Effect of Single-Qubit Gates on state |-i>


Goal

Create quantum circuits to apply various single qubit gates on state |-i> and understand the change in state and phase of the qubit.

X gate applied to -i stateY gate applied to -i stateZ gate applied to -i stateS gate applied to -i state

To see the effect of each of the gates, we will take a single circuit with 4 qubits and apply a different gate to each of the qubits to plot the values of each of those qubits on the blocsphere.

qc6 = QuantumCircuit(4) # initialize qubits qc6.x(range(4)) qc6.h(range(4)) qc6.s(range(4)) # perform gate operations on individual qubits qc6.x(0) qc6.y(1) qc6.z(2) qc6.s(3) # Draw circuit qc6.draw()
# Plot blochshere out6 = execute(qc6,backend).result().get_statevector() plot_bloch_multivector(out6)
Effect on Qubit on application of GateSatevectorQSphere PlotProbability (Histogram)
Input State = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}

Before measurement,
- qubit state = (00.707j0.707+0j)\begin{pmatrix}0-0.707j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0+0.707j)\begin{pmatrix}-0.707+0j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0+0.707j)\begin{pmatrix}0.707+0j & 0+0.707j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
Input State = (0.707+0j00.707j)\begin{pmatrix}0.707+0j & 0-0.707j\end{pmatrix}

Before measurement,
- qubit state = (0.707+0j0.707+0j)\begin{pmatrix}0.707+0j & 0.707+0j\end{pmatrix}
- qubit has probability 0.5 of being in each of the states ‘0’ and ‘1’
altaltalt
import qiskit qiskit.__qiskit_version__