In [1]:
from qiskit import *
In [2]:
circuit = QuantumCircuit(2,2) # Register two qubits and two classical bits
In [3]:
#quantum_register = QuantumRegister(2)
#classical_register = ClassicalRegister(2)
#circuit = QuantumCircuit(quantum_register, classical_register)
In [4]:
circuit.draw()
Out[4]:
q_0:
q_1:
c: 2/
In [5]:
%matplotlib inline
In [6]:
circuit.draw(output='mpl')
Out[6]:
In [7]:
# Hadamard Gate
circuit.h(0)
circuit.draw(output='mpl')
Out[7]:
In [8]:
# CNOT Gate
circuit.cx(0,1) # 0-> control qubit, 1-> target qubit
circuit.measure([0,1],[0,1]) # Measure q0,q1 and associate them with c0,c1
circuit.draw(output='mpl')
Out[8]:
In [9]:
from qiskit_aer import Aer
simulator = Aer.get_backend("qasm_simulator")
In [10]:
#result = transpile(circuit,backend=simulator)
In [11]:
from qiskit.visualization import plot_histogram
In [12]:
#plot_histogram(result.get_counts(circuit))
In [13]:
#Most generally, assemble() is used when working with Aer, where as transpile() used when working with IBMQ Provider.
#qobj = assemble(circuit) # Assemble circuit into a Qobj that can be run
#counts = simulator.run(qobj).result().get_counts() # Do the simulation, returning the state vector
# ==> Works like this too but it will be depreciated soon
counts=simulator.run(circuit).result().get_counts()
plot_histogram(counts) # Display the output on measurement of state vector
Out[13]:
In [14]:
# In order to get probabilities instead of counts
from qiskit.visualization import plot_distribution
plot_distribution(counts)
Out[14]:
In [15]:
from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService(name="open")
In [16]:
quantum_computer = service.get_backend("ibm_sherbrooke")
In [17]:
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler
In [18]:
pm = generate_preset_pass_manager(backend=quantum_computer, target=quantum_computer.target, optimization_level=1)
isa_circuit = pm.run(circuit)
sampler = Sampler(quantum_computer)
job = sampler.run([isa_circuit])
result = job.result()
In [19]:
#counts=quantum_computer.run(circuit).result().get_counts()
In [20]:
# pub_result = job_result[<idx>].data.<classical register>.get_counts()
# where <idx> is the index of the pub and <classical register> is the name of the classical register.
# You can use circuit.cregs to find the name of the classical registers.
pub_result = result[0].data.c.get_counts()
In [21]:
plot_histogram(pub_result)
Out[21]:
In [22]:
circuit.cregs
Out[22]:
[ClassicalRegister(2, 'c')]
In [23]:
plot_distribution(pub_result)
Out[23]: