import qiskit
from qiskit import *
print(qiskit.__version__)

%matplotlib inline
from qiskit.tools.visualization import plot_histogram
from ibm_quantum_widgets import draw_circuit

# initialize two qubits in the zero state and 
# two classical bits in the zero state in the quantum circuit
circuit = QuantumCircuit(3,3)

# C gate
# (2) => q2
circuit.x(2)
circuit.draw(output='mpl')

draw_circuit(circuit)

# Hadamard (H) gate
# (0) => (q0)
circuit.h(0)
circuit.draw(output='mpl')

draw_circuit(circuit)

# A controlled-NOT (CX NOT) gate, on control qubit 0
# and target qubit 1, putting the qubits in an entangled state.

# (2,1) = > q2(control bit)(quantum bit)(origin) --> q1(target bit)(quantum bit)(destination)
circuit.cx(2,1)
# (0,2) = > q0(control bit)(quantum bit)(origin) --> q2(target bit)(quantum bit)(destination)
circuit.cx(0,2)
circuit.draw(output='mpl')

draw_circuit(circuit)

# measurement between quantum state and classical state
# [1,1] => [q1,q1](quantum bit) and [2,0] => [c2,c0](classical bit) 
circuit.measure([1,1],[2,0])
circuit.draw(output='mpl')

draw_circuit(circuit)

# The n qubit’s measurement result will be stored in the n classical bit
circuit.measure([1,1,2],[2,0,0])
circuit.draw(output='mpl')

draw_circuit(circuit)

simulator = Aer.get_backend("qasm_simulator")

job = execute(circuit, simulator, shots=2024)

result = job.result()

counts = result.get_counts(circuit)

print("Total count for 100 and 101 are: ", counts)

# 1.
plot_histogram(counts)

# 2.
from ibm_quantum_widgets import draw_circuit

draw_circuit(circuit)

