In [1]:
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
Use Aer's AerSimulator
In [2]:
simulator = AerSimulator()
Create a Quantum Circuit acting on the q register
In [8]:
circuit = QuantumCircuit(2)
Add a H gate on qubit 0
In [9]:
circuit.h(0)
circuit.draw()
Out[9]:
┌───┐
q_0: ┤ H ├
└───┘
q_1: ─────
Add a CX (CNOT) gate on control qubit 0 and target qubit 1
In [10]:
circuit.cx(0, 1)
circuit.draw()
Out[10]:
┌───┐
q_0: ┤ H ├──■──
└───┘┌─┴─┐
q_1: ─────┤ X ├
└───┘
Map the quantum measurement to the classical bits
In [11]:
circuit.measure_all()
In [12]:
print(circuit)
circuit.draw(output='mpl')
┌───┐ ░ ┌─┐
q_0: ┤ H ├──■───░─┤M├───
└───┘┌─┴─┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├─░──╫─┤M├
└───┘ ░ ║ └╥┘
meas: 2/══════════════╩══╩═
0 1
Out[12]:
Compile the circuit for the support instruction set (basis_gates)
and topology (coupling_map) of the backend
In [14]:
compiled_circuit = transpile(circuit, simulator)
print(compiled_circuit)
┌───┐ ░ ┌─┐
q_0: ┤ H ├──■───░─┤M├───
└───┘┌─┴─┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├─░──╫─┤M├
└───┘ ░ ║ └╥┘
meas: 2/══════════════╩══╩═
0 1
Execute the circuit on the aer simulator
In [15]:
job = simulator.run(compiled_circuit, shots=1000)
Grab results from the job
In [16]:
result = job.result()
result
Out[16]:
Result(backend_name='aer_simulator', backend_version='0.17.0', qobj_id='', job_id='426e9665-2543-445b-bf76-716759076a62', success=True, results=[ExperimentResult(shots=1000, success=True, meas_level=2, data=ExperimentResultData(counts={'0x0': 506, '0x3': 494}), header=QobjExperimentHeader(creg_sizes=[['meas', 2]], global_phase=0.0, memory_slots=2, n_qubits=2, name='circuit-164', qreg_sizes=[['q', 2]], metadata={}), status=DONE, seed_simulator=1836436501, metadata={'time_taken': 0.014929336, 'num_bind_params': 1, 'parallel_state_update': 8, 'parallel_shots': 1, 'required_memory_mb': 0, 'input_qubit_map': [[1, 1], [0, 0]], 'method': 'stabilizer', 'device': 'CPU', 'num_qubits': 2, 'sample_measure_time': 0.000636645, 'active_input_qubits': [0, 1], 'num_clbits': 2, 'remapped_qubits': False, 'runtime_parameter_bind': False, 'max_memory_mb': 15673, 'noise': 'ideal', 'measure_sampling': True, 'batched_shots_optimization': False, 'fusion': {'enabled': False}}, time_taken=0.014929336)], date=2025-05-24T12:42:53.512793, status=COMPLETED, header=None, metadata={'time_taken_parameter_binding': 1.4814e-05, 'time_taken_execute': 0.014977165, 'omp_enabled': True, 'max_gpu_memory_mb': 0, 'max_memory_mb': 15673, 'parallel_experiments': 1}, time_taken=0.016008377075195312)
Returns counts
In [17]:
counts = result.get_counts()
print("\nTotal count for 00 and 11 are:", counts)
Total count for 00 and 11 are: {'00': 506, '11': 494}
In [12]:
plot_histogram(counts) # |00> and |11> should have equal probability
Out[12]:
In [19]:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
qr = QuantumRegister(3, 'q')
anc = QuantumRegister(1, 'ancilla')
cr = ClassicalRegister(3, 'c')
qc = QuantumCircuit(qr, anc, cr)
qc.x(anc[0])
qc.h(anc[0])
qc.h(qr[0:3])
qc.cx(qr[0:3], anc[0])
# qc.barrier(qr)
qc.h(qr[0:3])
qc.barrier(qr)
qc.measure(qr, cr)# |1> -> |0> - |1>
qc.draw('mpl')
Out[19]:
In [20]:
transpiled_qc = transpile(qc, simulator)
In [21]:
job = simulator.run(transpiled_qc, shots=2048)
In [22]:
result = job.result()
In [23]:
counts = result.get_counts()
In [24]:
plot_histogram(counts)
Out[24]:
In [ ]: