#!/usr/bin/env python3 
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute

from qiskit.visualization import plot_bloch_multivector, plot_histogram

import numpy as np
import pylab as plt

import sys
sys.path.append("/Users/gshyam/projects/work_projects/machine_learning/QuantumComputing/quantum_computing/utils")
from utils import get_statevector

def get_state_bell_ckt(qc):
    backend = Aer.get_backend('statevector_simulator')
    res  = execute(qc, backend=backend).result()
    final_state = res.get_statevector()
    counts = res.get_counts()
    return (final_state, counts)

qc1 = QuantumCircuit(2)
qc1.h(0)
qc1.cx(0,1)
fig, ax = plt.subplots(1, 2, constrained_layout=True)
qc1.draw(output='mpl', ax=ax[0], scale=1.5)
sv, counts = get_state_bell_ckt(qc1)
plot_histogram(counts, ax=ax[1])

qc1 = QuantumCircuit(2)
qc1.h(0)
qc1.cx(0,1)
qc1.draw(output='mpl')

backend = Aer.get_backend('statevector_simulator')
res  = execute(qc1, backend=backend).result()
final_state = res.get_statevector()
counts = res.get_counts()
print (final_state, counts)

plot_histogram(counts)


qc2 = QuantumCircuit(2)
qc2.h(0)
qc2.cx(0,1)
qc2.x(0)
qc2.draw(output='mpl')

backend = Aer.get_backend('statevector_simulator')
res  = execute(qc2, backend=backend).result()
final_state = res.get_statevector()
counts = res.get_counts()
print (final_state, counts)

plot_histogram(counts)


qc3 = QuantumCircuit(2)
qc3.h(0)
qc3.cx(0,1)
qc3.x(1)
qc3.draw(output='mpl')

backend = Aer.get_backend('statevector_simulator')
res  = execute(qc3, backend=backend).result()
final_state = res.get_statevector()
counts = res.get_counts()
print (final_state, counts)

plot_histogram(counts)


qc4 = QuantumCircuit(2)
qc4.h(0)
qc4.cx(0,1)
qc4.x(1)
qc4.x(0)
qc4.draw(output='mpl')

backend = Aer.get_backend('statevector_simulator')
res  = execute(qc4, backend=backend).result()
final_state = res.get_statevector()
counts = res.get_counts()
print (final_state, counts)

plot_histogram(counts)






#!/usr/bin/env python3 
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute

from qiskit.visualization import plot_histogram

# Define quantum circuit

N_qubits=2; N_bits=2;
bell= QuantumCircuit(N_qubits, N_bits);
bell.h(0)      # Add a hadamard
bell.cx(0, 1)  # add a Cnot gate # similar to Last week's discussion
# Measure the result : Syntax QuantumCircuit.measure(qubit, cbit)
#bell.measure([0,1],[0,1])
#bell.draw()
bell.draw(output='mpl')

for backend in Aer.backends():
    print(backend.name() )

backend = Aer.get_backend('unitary_simulator')
job_uni = execute(bell, backend)
res_uni = job_uni.result()
print(res_uni.get_unitary(bell, decimals=1))

bell.measure([0,1],[0,1])
backend = Aer.get_backend('unitary_simulator')
N=2
q=QuantumRegister(N)
c=ClassicalRegister(N)
circ = QuantumCircuit(q,c)
circ.h(q[0])
circ.cx(q[0],q[1])
circ.h(q[1])

job = execute(circ, backend)
result = job.result()
print(result.get_unitary(circ, decimals=1))

sim_qsm = Aer.get_backend('qasm_simulator')
job_qsm = execute(bell, sim_qsm, shots=1024) # shots=1024 (default)
res_qsm = job_qsm.result()
print (res_qsm)


counts_qsm = res_qsm.get_counts(bell)
print (counts_qsm)
plot_histogram( counts_qsm, title='Bell-State counts')

# Define quantum circuit

N_qubits=2; N_bits=2;
ckt2= QuantumCircuit(N_qubits, N_bits);

ckt2.h(0)
ckt2.cx(0, 1)
ckt2.h(1)  # this time on quibit 1
ckt2.cx(0, 1)
ckt2.measure([0,1],[0,1])
#ckt2.draw()
ckt2.draw(output='mpl', style = {'backgroundcolor':'lightblue'})

sim_qsm = Aer.get_backend('qasm_simulator')
job_qsm = execute(ckt2, sim_qsm, shots=2000)
res_qsm = job_qsm.result()
counts_qsm = res_qsm.get_counts(ckt2)
print (counts_qsm)
plot_histogram( counts_qsm, title='Bell-State counts')

# Define quantum circuit
import pylab as plt

%matplotlib inline

def ckt_(N, init_state, axis):
    q=QuantumRegister(N)
    c=ClassicalRegister(N)
    ckt3= QuantumCircuit(q, c);
    #print (q, c, q[0], c[0], q[1], q[2])

    ckt3.h(q[init_state])
    ckt3.cx(q[0], q[1])
    ckt3.h(q[1])  # this time on quibit 1
    ckt3.cx(q[1], q[2])
    ckt3.h(q[2])
    ckt3.cx(q[0], q[2])
    ckt3.measure(q, c)
    ckt3.draw(output='mpl', ax=axis)
    

fig, ax = plt.subplots(1, 2) #figsize=(7, 7)
ckt_(3, 0, axis=ax[0])
ckt_(3, 1, axis=ax[1])

N=3;
q=QuantumRegister(N)
c=ClassicalRegister(N)
ckt3= QuantumCircuit(q, c, name='ckt3');

ckt3.h(q[0])
ckt3.cx(q[0], q[1])
ckt3.h(q[1])
ckt3.cx(q[1], q[2])
ckt3.h(q[2])
ckt3.cx(q[0], q[2])
ckt3.measure(q, c)
ckt3.draw(output='mpl')


sim_uni = Aer.get_backend('unitary_simulator')
job_uni = execute(ckt3, sim_uni)
res_uni = job_uni.result()
print (res_uni)

#print (res_uni.get_unitary(ckt3,decimals=1))

sim_qsm = Aer.get_backend('qasm_simulator')
job_qsm = execute(ckt3, sim_qsm, shots=2000)
res_qsm = job_qsm.result()
counts_qsm = res_qsm.get_counts(ckt3)
print (counts_qsm, sum(counts_qsm.values()))
plot_histogram( counts_qsm, title='Bell-State counts')
#ckt3.to_matrix()

N=4;
q=QuantumRegister(N)
c=ClassicalRegister(N)
circ = QuantumCircuit(q, c);

for i in range(0, N-1):
    circ.h(q[i])
    circ.cx(q[i], q[i+1])

circ.measure(q, c)
sim_qsm = Aer.get_backend('qasm_simulator')
job_qsm = execute(circ, sim_qsm, shots=2000)
res_qsm = job_qsm.result()
counts_qsm = res_qsm.get_counts(circ)
print (len(counts_qsm.values()))
print("Time taken: {} sec".format(res_qsm.time_taken))
%matplotlib inline
plot_histogram( counts_qsm)

