from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code for QISKit exercises."></form>''')

from qiskit import *

# Quantum program setup 
Q_program = QuantumProgram()

# Creating registers
q = Q_program.create_quantum_register('q', 3)
c0 = Q_program.create_classical_register('c0', 1)
c1 = Q_program.create_classical_register('c1', 1)
c2 = Q_program.create_classical_register('c2', 1)

# Creates the quantum circuit
teleport = Q_program.create_circuit('teleport', [q], [c0,c1,c2])

# Make the shared entangled state 
teleport.h(q[1])
teleport.cx(q[1], q[2])

# Prepare Alice's qubit
teleport.h(q[0])

# Alice applies teleportation gates (or projects to Bell basis)
teleport.cx(q[0], q[1])
teleport.h(q[0])

# Alice measures her qubits
teleport.measure(q[0], c0[0])
teleport.measure(q[1], c1[0])

# Bob applies certain gates based on the outcome of Alice's measurements
teleport.z(q[2]).c_if(c0, 1)
teleport.x(q[2]).c_if(c1, 1)

# Bob checks the state of the teleported qubit
teleport.measure(q[2], c2[0])

# Shows gates of the circuit
circuits = ['teleport']
print(Q_program.get_qasms(circuits)[0])

# Parameters for execution on simulator
backend = 'local_qasm_simulator' 
shots = 1024 # the number of shots in the experiment 

# Run the algorithm
result = Q_program.execute(circuits, backend=backend, shots=shots)

#Shows the results obtained from the quantum algorithm 
counts = result.get_counts('teleport')

print('\nThe measured outcomes of the circuits are:', counts)

# credits to:  https://github.com/QISKit/qiskit-tutorial

from initialize import *
from qiskit import *


#initialize quantum program
my_alg = initialize(circuit_name = 'superdense', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)

#add gates to the circuit

#creates a bell pair
my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate

#Alice encodes 01
my_alg.q_circuit.x(my_alg.q_reg[0])

#to measure in the Bell basis, Bob does the following operations before measuring in the standard basis
my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate
my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit
my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit


print('List of gates:')
for circuit in my_alg.q_circuit:
    print(circuit.name)

#Execute the quantum algorithm
result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)

#Show the results obtained from the quantum algorithm 
counts = result.get_counts(my_alg.circ_name)

print('\nThe measured outcomes of the circuits are:',counts)

# credits to:  https://github.com/QISKit/qiskit-tutorial