In [1]:
from qiskit import *
from qiskit.visualization import plot_bloch_multivector
from qiskit.visualization import plot_histogram
%matplotlib inline
import math
In [2]:
from qiskit_aer import Aer
Aer.backends()
Out[2]:
[AerSimulator('aer_simulator'),
AerSimulator('aer_simulator_statevector'),
AerSimulator('aer_simulator_density_matrix'),
AerSimulator('aer_simulator_stabilizer'),
AerSimulator('aer_simulator_matrix_product_state'),
AerSimulator('aer_simulator_extended_stabilizer'),
AerSimulator('aer_simulator_unitary'),
AerSimulator('aer_simulator_superop'),
QasmSimulator('qasm_simulator'),
StatevectorSimulator('statevector_simulator'),
UnitarySimulator('unitary_simulator')]
In [3]:
qasm_simulator = Aer.get_backend("qasm_simulator")
statevector_simulator = Aer.get_backend("statevector_simulator")
In [4]:
def run_on_simulators(circuit):
statevec_job = statevector_simulator.run(circuit)
result = statevec_job.result()
statevec = result.get_statevector()
num_qubits = circuit.num_qubits #used this just to show such a function exists to get a number of qubits
# circuit.measure([0,1],[0,1]) # Measure q0,q1 and associate them with c0,c1
circuit.measure([i for i in range(num_qubits)],[i for i in range(num_qubits)])
qasm_job = qasm_simulator.run(circuit, shots=1024).result()
counts = qasm_job.get_counts()
return statevec, counts
In [5]:
circuit = QuantumCircuit(2,2)
statevec, counts = run_on_simulators(circuit)
In [6]:
# Plot bloch sphere representation of qubits
plot_bloch_multivector(statevec)
Out[6]:
In [7]:
# Plot bloch sphere representation of qubits after applying Hadamard gate to the first qubit
circuit.h(0)
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[7]:
In [8]:
plot_histogram(counts)
Out[8]:
In [9]:
# Plot bloch sphere representation of qubits after applying Hadamard gate to the first qubit, then applying CNOT gate
# Since the qubits will be entangled, the result on bloch sphere would not be so sensible.
# In this case, for example, it makes sense to look at the Q sphere.
circuit = QuantumCircuit(2,2)
circuit.h(0)
circuit.cx(0,1) # 0-> control qubit, 1-> target qubit
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[9]:
In [10]:
plot_histogram(counts)
Out[10]:
In [11]:
# Plot bloch sphere representation of qubits after rotating around x axis
circuit = QuantumCircuit(2,2)
circuit.rx(math.pi/4, 0) #Rotate Pi/4 around x axis, apply on the first qubit
circuit.rx(math.pi/2, 1) #Rotate Pi/2 around x axis, apply on the second qubit
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[11]:
In [12]:
# Plot bloch sphere representation of qubits after rotating around y axis
# When pi/2 in this case, it would give the same result as applying the Hadamard gate.
circuit = QuantumCircuit(2,2)
circuit.ry(math.pi/4, 0) #Rotate Pi/4 around x axis, apply on the first qubit
circuit.ry(math.pi/2, 1) #Rotate Pi/2 around x axis, apply on the second qubit
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[12]:
In [13]:
# Apply Hadamard gate to show the similarity to pi/2 rotation in y-axis
circuit = QuantumCircuit(1,1)
circuit.h(0)
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[13]:
In [14]:
# Apply Hadamard gate and then Z gate
circuit = QuantumCircuit(1,1)
circuit.h(0)
circuit.z(0)
statevec, counts = run_on_simulators(circuit)
plot_bloch_multivector(statevec)
Out[14]:
In [15]:
plot_histogram(counts)
Out[15]:
In [16]:
# If I apply Hadamard gate to the first qubit, then apply CNOT, it would entagle the qubits.
# To reverse this operation, I would apply one more CNOT and then Hadamard gate. This would put it back to 00.
# What happens if we add a Z gate between two CNOTs? It would give 01, in other words, it would alter the rotation.
# Thus, phase may or may not affect the outcome