from qiskit import *
from qiskit.quantum_info import Statevector

def qasm_sim(qc):
    backend = Aer.get_backend('qasm_simulator')
    job = execute(qc, backend, shots=1024)
    result = job.result()
    counts = result.get_counts(qc)
    return counts

qc=QuantumCircuit(2)
qc.x(0)
qc.h(1)
qc.draw(output="mpl")

qc.measure_all()
qc.draw(output="mpl")

qc.size()

qc.clbits

qc=QuantumCircuit(2)
qc.x(0)
qc.h(1)
qc.draw(output="mpl")

qc=QuantumCircuit(2,2)   # 2 qubits and 2 bits
qc.x(0)
qc.h(1)
qc.draw(output="mpl")

qc.measure(0,0)
qc.measure(1,1)
qc.draw(output="mpl")

qc=QuantumCircuit(2,2)   # 2 qubits and 2 bits
qc.x(0)
qc.h(1)
qc.draw(output="mpl")

qc.measure([0,1],[0,1])
qc.draw(output="mpl")

qc=QuantumCircuit(2)
qc.measure_all()
qc.draw(output="mpl")

backend = Aer.get_backend('qasm_simulator')
job=execute (qc, backend)
result = job.result()
counts = result.get_counts()
print(counts)

qc=QuantumCircuit(2,2)   # 2 qubits and 2 bits
qc.x(0)
qc.h(1)
qc.measure(range(2),range(2))
qc.draw(output="mpl")

q=QuantumRegister(2,'qr')
c=ClassicalRegister(2,'cr')
qc=QuantumCircuit(q,c)
qc.h(0)
qc.x(1)
qc.draw(output="mpl")

qc=QuantumCircuit(2,2)   # 2 qubits and 2 bits
qc.x(0)
qc.h(1)
qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qc.reset(1)

qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qc=QuantumCircuit(1)   
qc.x(0)
qc.h(0)
qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qc=QuantumCircuit(1)   # 2 qubits and 2 bits
qc.x(0)
qc.reset(0)
qc.h(0)
qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qr=QuantumRegister(2,'qr')
cr=ClassicalRegister(2,'cr')

qc=QuantumCircuit(qr,cr)   # 2 qubits and 2 bits
qc.x(0)
qc.x(1)
qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qc.s(qr[1]).c_if(cr,0)

qc.draw(output="mpl")

state = Statevector.from_instruction(qc)
state.draw(output='latex')

qr=QuantumRegister(2,'qr')
cr=ClassicalRegister(2,'cr')

qc=QuantumCircuit(qr,cr)   # 2 qubits and 2 bits
qc.x(0)

qc.measure([0,1],[0,1])
qc.draw(output="mpl")

qasm_sim(qc)

qc.s(qr[0]).c_if(cr,1)
qc.draw(output="mpl")

qasm_sim(qc)

qc.x(qr[1]).c_if(cr,1)
qc.measure([0,1],[0,1])
qc.draw(output="mpl")

qasm_sim(qc)