from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.providers.aer import AerProvider
from qiskit import execute

qr = QuantumRegister(2) # 2 qubits
cr = ClassicalRegister(2) # 2 classical bits
qc = QuantumCircuit(qr, cr) # a quantum circuit with 2 qubits and 2 bits

qc.draw()

qc.h(qr[0]) # 0번째 qubit에 hadamard 연산을 취합니다.
qc.cx(qr[0], qr[1]) # 0번째 qubit을 control, 1번째 qubit를 target으로 하는 CNOT 연산을 취합니다.
qc.measure(qr, cr) # qr을 측정하여 cr에 저장하는 측정 연산을 취합니다.

qc.draw('mpl') # 완성된 회로를 출력합니다.

qasm_simulator = AerProvider().get_backend('qasm_simulator')

job_qasm = execute(qc, backend=qasm_simulator, shots=2048)

# 다른 방법
# job_qasm = qasm_simulator.run(qc, shots=2048)
#
# 또는
#
# from qiskit.utils import QuantumInstance
# qi = QuantumInstance(qasm_simulator, shots=2048)
# result = qi.execute(qc)
# counts = result.get_counts()

counts = job_qasm.result().get_counts()

print("Bell measurement result")
for k, v in counts.items():
    print(k, v)

from qiskit.visualization import plot_histogram
plot_histogram(counts)