from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer,assemble, transpile
from qiskit import BasicAer
from random import randrange
from qiskit.tools.visualization import plot_histogram, plot_state_city
import numpy as np
from qiskit.quantum_info.operators import Operator, Pauli
from qiskit.quantum_info import Operator, Statevector

q = QuantumRegister(6)  # "Qubits q[0] and q[1] for coin 1 and 2", "q[2] and q[3] for player 1" and q[4] and q[5] for player 2" 
c = ClassicalRegister(6)
Q = QuantumCircuit(q,c)

#Coin Operators
# Apply u gate as Hadamard gate to each coin
Q.u(np.pi/2,0,np.pi,q[0])   
Q.u(np.pi/2,0,np.pi,q[1])

#S operator

Q.x(q[0])
Q.x(q[1])
Q.mct([q[0],q[1]],q[3])
Q.mct([q[0],q[1]],q[5])
Q.x(q[0])
Q.x(q[1])
Q.barrier()


Q.x(q[0])
Q.mct([q[0],q[1]],q[2])
Q.mct([q[0],q[1]],q[3])
Q.x(q[0])
Q.barrier()

Q.x(q[1])
Q.mct([q[0],q[1]],q[4])
Q.mct([q[0],q[1]],q[5])
Q.x(q[1])
Q.barrier()

Q.mct([q[0],q[1]],q[2])
Q.mct([q[0],q[1]],q[4])




Q.measure_all()

Q.draw(output='mpl')

# Execute the circuit on the qasm simulator
# Use Aer's qasm_simulator
backend_sim = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit
# to be 1024, which is the default.
job_sim = execute(Q, backend_sim, shots=1000)

# Grab the results from the job.
result_sim = job_sim.result()
counts = result_sim.get_counts(Q)
print(counts)
    


plot_histogram(counts, color='midnightblue', title="Decision Histogram")


q_o = QuantumRegister(6)  # "Qubits q[0] and q[1] for coin 1 and 2", "q[2] and q[3] for player 1" and q[4] and q[5] for player 2" 
c_o = ClassicalRegister(6)
Q_o = QuantumCircuit(q,c)

#Coin Operators
# Apply u gate as Hadamard gate to each coin
Q_o.u(2*np.pi/3,0,np.pi,q[0])   
Q_o.u(np.pi/2,0,np.pi,q[1])
#S operator

Q_o.x(q[0])
Q_o.x(q[1])
Q_o.mct([q[0],q[1]],q[3])
Q_o.mct([q[0],q[1]],q[5])
Q_o.x(q[0])
Q_o.x(q[1])
Q_o.barrier()


Q_o.x(q[0])
Q_o.mct([q[0],q[1]],q[2])
Q_o.mct([q[0],q[1]],q[3])
Q_o.x(q[0])
Q_o.barrier()

Q_o.x(q[1])
Q_o.mct([q[0],q[1]],q[4])
Q_o.mct([q[0],q[1]],q[5])
Q_o.x(q[1])
Q_o.barrier()

Q_o.mct([q[0],q[1]],q[2])
Q_o.mct([q[0],q[1]],q[4])




Q_o.measure_all()


# Execute the circuit on the qasm simulator
# Use Aer's qasm_simulator
backend_sim_o = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit
# to be 1024, which is the default.
job_sim_o = execute(Q_o, backend_sim, shots=1000)

# Grab the results from the job.
result_sim_o = job_sim_o.result()
counts_o = result_sim_o.get_counts(Q_o)
print(counts_o)
    


plot_histogram(counts_o, color='midnightblue', title="Decision Histogram")