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 initialize import *


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

#add gates to the circuit
my_alg.q_circuit.x(my_alg.q_reg[1]) # initializes ancilla qubit

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second qubit

my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate as the query function

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second qubit 

my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit

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)

from initialize import *


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

#add gates to the circuit
my_alg.q_circuit.x(my_alg.q_reg[2]) # initializes ancilla qubit

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second qubit
my_alg.q_circuit.h(my_alg.q_reg[2]) # applies H gate to third qubit

my_alg.q_circuit.cx(my_alg.q_reg[1],my_alg.q_reg[2]) ## applies CX gate as the query function
my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[2]) ## applies CX gate as the query function

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second qubit 
my_alg.q_circuit.h(my_alg.q_reg[2]) # applies H gate to third qubit

my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit and store the result in the first bit
my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit and store the result in the second bit

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)

from initialize import *


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

#add gates to the circuit

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second qubit

my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[2]) ## applies CX gate as the query function
my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[3]) ## applies CX gate as the query function
my_alg.q_circuit.cx(my_alg.q_reg[1],my_alg.q_reg[2]) ## applies CX gate as the query function
my_alg.q_circuit.cx(my_alg.q_reg[1],my_alg.q_reg[3]) ## applies CX gate as the query function

my_alg.q_circuit.measure(my_alg.q_reg[2], my_alg.c_reg[2]) # measures the third qubit 
my_alg.q_circuit.measure(my_alg.q_reg[3], my_alg.c_reg[3]) # measures the fourth qubit

my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit
my_alg.q_circuit.h(my_alg.q_reg[1]) # applies H gate to second 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)