%matplotlib notebook

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

qc = QuantumCircuit(2,2) # create an empty circuit with two qubits and two output bits

# create a circuit with the required measurements, so we can add them in easily
meas = QuantumCircuit(2,2)
meas.measure(0,0)
meas.measure(1,1)

# loop over the squares centered on (1,2) and (6,2) and make all dim
for (X,Y) in [(1,2),(6,2)]:
    for dX in [+1,0,-1]:
        for dY in [+1,0,-1]:
            screen.pixel(X+dX,Y+dY,2)
        
for (X,Y) in [(1,2),(6,2)]:
    screen.pixel(X,Y,0) # turn off the center pixels of the squares  

old_keys = 0
while True: # loop which checks for user input and responds

    # look for and act upon key presses
    keys = pew.keys() # get current key presses
    if keys!=0 and keys!=old_keys:
        if keys&pew.K_UP:
            qc.x(0) # x for qubit 0 when UP is pressed
        if keys&pew.K_LEFT:
            qc.x(1) # x for qubit 1 when LEFT is pressed
    old_keys = keys 
    
    # execute the circuit and get a single sample of memory
    m = simulate(qc+meas,shots=1,get='memory')
    
    # turn the pixel (1,2) on or off depending on whether the first bit value is 1 or 0
    if m[0][0]=='1':
        screen.pixel(1,2,3)
    else:
        screen.pixel(1,2,0)
    # do the same for pixel (6,2)
    if m[0][1]=='1':
        screen.pixel(6,2,3)
    else:
        screen.pixel(6,2,0)

    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

# create an empty circuit with two qubits and two output bits
qc = QuantumCircuit(2,2) 

cnot = QuantumCircuit(2,2)
cnot.cx(0,1)

# create a circuit with the required measurements, so we can add them in easily
meas = QuantumCircuit(2,2)
meas.measure(0,0)
meas.measure(1,1)

# loop over the square centered on (1,2) and make all dim
for (X,Y) in [(1,2),(6,2)]:
    for dX in [+1,0,-1]:
        for dY in [+1,0,-1]:
            screen.pixel(X+dX,Y+dY,2)
    
for (X,Y) in [(1,2),(6,2)]:
    screen.pixel(X,Y,0) # turn off the center pixels of the squares       

old_keys = 0
while True: # loop which checks for user input and responds

    # look for and act upon key presses
    keys = pew.keys() # get current key presses
    if keys!=0 and keys!=old_keys:
        if keys&pew.K_UP:
            qc.x(0) # x for qubit 0 when UP is pressed
        if keys&pew.K_LEFT:
            qc.x(1) # x for qubit 1 when LEFT is pressed
    old_keys = keys 

    # execute the circuit and get a single sample of memory
    m = simulate(qc+meas,shots=1,get='memory')
    
    # turn the pixel (1,2) on or off depending on whether the first bit value is 1 or 0
    if m[0][0]=='1':
        screen.pixel(1,2,3)
    else:
        screen.pixel(1,2,0)
    # do the same for pixel (6,2)
    if m[0][1]=='1':
        screen.pixel(6,2,3)
    else:
        screen.pixel(6,2,0)
        
    m = simulate(qc+cnot+meas,shots=1,get='memory')
    xor = m[0][0]
    
    if xor=='0':
        for (X,Y) in [(3,2),(4,2)]:
            screen.pixel(X,Y,0)
    else:
        for (X,Y) in [(3,2),(4,2)]:
            screen.pixel(X,Y,3)
        
    pew.show(screen) # update screen to display any changes

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

