%matplotlib notebook

import pew # setting up tools for the pewpew

pew.init() # initialize the game engine...
screen = pew.Pix() # ...and the screen

# fill the screen with medium brightness pixels
for X in range(8):
    for Y in range(8):
        screen.pixel(X,Y,2)

B = 3 # set brightness
screen.pixel(6,6,B) # put a bright pixel at (6,6)

pew.show(screen) # update screen to display the above changes

pew.tick(5) # pause for 5 seconds before quitting

import pew # setting up tools for the pewpew

pew.init() # initialize the game engine...
screen = pew.Pix() # ...and the screen

# fill the screen with medium brightness pixels
for X in range(8):
    for Y in range(8):
        screen.pixel(X,Y,2)
        
B = 3 # set initial brightness

pressing = False
while True: # loop which checks for user input and responds

    keys = pew.keys() # get current key presses
    if not pressing:
        if keys&pew.K_X: # pressing X turns off the program
            break
        if keys&pew.K_UP: # if UP is pressed, increase brightness of pixel at at (6,6)
            B = min(B+1,3)
        if keys&pew.K_DOWN: # if DOWN is pressed, decrease brightness of pixel at at (6,6)
            B = max(B-1,0)
        if keys:
            pressing = True
    else:
        if not keys:
            pressing = False
            
    screen.pixel(6,6,B) # put a pixel at (6,6) with current brightness

    pew.show(screen) # update screen to display the above changes

    pew.tick(1/6) # pause for a sixth of a second

import pew # setting up tools for the pewpew
from microqiskit import QuantumCircuit, simulate # setting up tools for quantum

pew.init() # initialize the game engine...
screen = pew.Pix() # ...and the screen

# fill the screen with dim pixels
for X in range(8):
    for Y in range(8):
        screen.pixel(X,Y,2)
        
pew.show(screen) # update screen to display the above changes

qc = QuantumCircuit(1,1) # create an empty single qubit circuit

# create a circuit that measures the qubit in the z basis
m_z = QuantumCircuit(1,1)
m_z.measure(0,0)

pressing = False
while True: # loop which checks for user input and responds

    keys = pew.keys() # get current key presses
    if not pressing:
        if keys&pew.K_X: # pressing X turns off the program
            break
        if keys&pew.K_UP: # if UP is pressed, add an x gate
            qc.x(0)
        if keys&pew.K_DOWN: # if DOWN is pressed, add an h gate
            qc.h(0)
        if keys:
            pressing = True
    else:
        if not keys:
            pressing = False
     
    # get the output from the current circuit
    full_circuit = qc + m_z
    bit = simulate(full_circuit,shots=1,get='memory')[0]
    
    # set the brightness of 6,6 according to the output
    if bit=='1':
        B = 3
    else:
        B = 0  
    
    screen.pixel(6,6,B) # put a pixel at (6,1) with current brightness

    pew.show(screen) # update screen to display the above changes

    pew.tick(1/6) # pause for a sixth of a second

import pew # setting up tools for the pewpew
from microqiskit import QuantumCircuit, simulate # setting up tools for quantum
from math import pi

pew.init() # initialize the game engine...
screen = pew.Pix() # ...and the screen

# fill the screen with dim pixels
for X in range(8):
    for Y in range(8):
        screen.pixel(X,Y,2)
        
pew.show(screen) # update screen to display the above changes

qc = QuantumCircuit(1,1) # create an empty single qubit circuit

# create a circuit that measures the qubit in the z basis
m_z = QuantumCircuit(1,1)
m_z.measure(0,0)

# create a circuit that measures the qubit in the x basis
m_x = QuantumCircuit(1,1)
m_x.h(0)
m_x.measure(0,0)

basis = 'z' # set initial measurement basis

pressing = False
while True: # loop which checks for user input and responds

    keys = pew.keys() # get current key presses
    if not pressing:
        if keys&pew.K_X: # pressing X turns off the program
            break
        if keys&pew.K_UP: # if UP is pressed, add an x gate
            qc.x(0)
        if keys&pew.K_DOWN: # if DOWN is pressed, add an h gate
            qc.h(0) 
        if keys&pew.K_O:
            basis = (basis=='z')*'x' + (basis=='x')*'z'
        if keys:
            pressing = True
    else:
        if not keys:
            pressing = False
     
    # get the output from the current circuit
    if basis=='z':
        full_circuit = qc + m_z
    elif basis=='x':
        full_circuit = qc + m_x
    bit = simulate(full_circuit,shots=1,get='memory')[0]
    
    # set the brightness of 6,6 according to the output
    if bit=='1':
        B = 3
    else:
        B = 0
    if basis=='z':
        screen.pixel(6,6,B)
        screen.pixel(1,1,2)
    elif basis=='x':
        screen.pixel(6,6,2)
        screen.pixel(1,1,B)

    pew.show(screen) # update screen to display the above changes

    pew.tick(1/6) # pause for a sixth of a second

import pew # setting up tools for the pewpew
from microqiskit import QuantumCircuit, simulate # setting up tools for quantum
from math import pi

pew.init() # initialize the game engine...
screen = pew.Pix() # ...and the screen

# fill the screen with dim pixels
for X in range(8):
    for Y in range(8):
        screen.pixel(X,Y,2)
        
pew.show(screen) # update screen to display the above changes

qc = QuantumCircuit(1,1) # create an empty single qubit circuit

# create a circuit that measures the qubit in the z basis
m_z = QuantumCircuit(1,1)
m_z.measure(0,0)

# create a circuit that measures the qubit in the x basis
m_x = QuantumCircuit(1,1)
m_x.h(0)
m_x.measure(0,0)

# create a circuit that measures the qubit in the y basis
m_y = QuantumCircuit(1,1)
m_y.rx(pi/2,0)
m_y.measure(0,0)

basis = 'z' # set initial measurement basis

pressing = False
while True: # loop which checks for user input and responds

    keys = pew.keys() # get current key presses
    if not pressing:
        if keys&pew.K_X: # pressing X turns off the program
            break
        if keys&pew.K_UP: # if UP is pressed, add an x gate
            qc.x(0)
        if keys&pew.K_DOWN: # if DOWN is pressed, add an h gate
            qc.h(0)
        if keys&pew.K_LEFT: # if LEFT is pressed, add a sqrt(x) gate
            qc.rx(pi/2,0)
        if keys&pew.K_O:
            basis = (basis=='z')*'x' + (basis=='x')*'y'+ (basis=='y')*'z'
        if keys:
            pressing = True
    else:
        if not keys:
            pressing = False
     
    # get the output from the current circuit
    if basis=='z':
        full_circuit = qc + m_z
    elif basis=='x':
        full_circuit = qc + m_x
    elif basis=='y':
        full_circuit = qc + m_y
    bit = simulate(full_circuit,shots=1,get='memory')[0]
    
    # set the brightness of 6,6 according to the output
    if bit=='1':
        B = 3
    else:
        B = 0
    if basis=='z':
        screen.pixel(6,6,B)
        screen.pixel(6,1,2)
        screen.pixel(1,1,2)
    elif basis=='x':
        screen.pixel(6,6,2)
        screen.pixel(6,1,2)
        screen.pixel(1,1,B)
    elif basis=='y':
        screen.pixel(6,6,2)
        screen.pixel(6,1,B)
        screen.pixel(1,1,2)

    pew.show(screen) # update screen to display the above changes

    pew.tick(1/6) # pause for a sixth of a second

%matplotlib inline
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *

# Set up tools for visualizing the Bloch sphere
from math import pi
def get_bloch(qc):
    
    # create a circuit that measures the qubit in the z basis
    m_z = QuantumCircuit(1,1)
    m_z.measure(0,0)

    # create a circuit that measures the qubit in the x basis
    m_x = QuantumCircuit(1,1)
    m_x.h(0)
    m_x.measure(0,0)

    # create a circuit that measures the qubit in the y basis
    m_y = QuantumCircuit(1,1)
    m_y.rx(pi/2,0)
    m_y.measure(0,0)

    shots = 2**14 # number of samples used for statistics

    bloch_vector = []
    # look at each possible measurement
    for measure_circuit in [m_x, m_y, m_z]:

        # run the circuit with a the selected measurement and get the number of samples that output each bit value
        counts = execute(qc+measure_circuit,Aer.get_backend('qasm_simulator'),shots=shots).result().get_counts()

        # calculate the probabilities for each bit value
        probs = {}
        for output in ['0','1']:
            if output in counts:
                probs[output] = counts[output]/shots
            else:
                probs[output] = 0

        # the bloch vector needs the different between these values
        bloch_vector.append( probs['0'] -  probs['1'] )
        
    return bloch_vector

qc = QuantumCircuit(1,1)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.x(0)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.h(0)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.x(0)
qc.h(0)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(-pi/2,0)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi/2,0)

plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*1/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*2/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*3/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
qc.rz(pi*1/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
qc.rz(pi*2/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
qc.rz(pi*3/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
qc.rz(pi*4/8,0)
plot_bloch_vector(get_bloch(qc))

qc = QuantumCircuit(1,1)
qc.rx(pi*4/8,0)
qc.rz(pi*4/8,0)
qc.ry(-pi*1/8,0)
plot_bloch_vector(get_bloch(qc))

