Spaces:
Runtime error
Runtime error
File size: 2,295 Bytes
8757ddd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import qiskit
import numpy as np
from qiskit import QuantumCircuit,Aer
import streamlit as st
def superposition():
ckt=QuantumCircuit(1,1)
ckt.h(0) #HedaMart gate
ckt.measure(0,0)
sim=Aer.get_backend('aer_simulator') # A simulator
result=sim.run(ckt).result().get_counts()
return result
def getRandom():
res=superposition()
#print(res)
values=list(res.values())
keys= list(res.keys())
#print(values,keys,sep='\n')
#replace by random vars
randomVal= '|' + str(keys[np.argmax(values)]) + '>'
return randomVal
#Tic Tac Toe Logic
def validate(arr):
"""
To check whether the game is finished or not
Winning condition --> 0
else return 1
"""
flag=True
zero_ket='|0>'
one_ket='|1>'
#diagonals
if arr[0,0]==one_ket and arr[1,1]==one_ket and arr[2,2]==one_ket:
st.success('User has won !!')
flag=False
elif arr[0,0]==zero_ket and arr[1,1]==zero_ket and arr[2,2]==zero_ket:
st.success('Computer has won !!')
flag=False
#2nd digonals
if arr[0,2]==one_ket and arr[1,1]==one_ket and arr[2,0]==one_ket:
st.success('User has won !!')
flag=False
elif arr[0,2]==zero_ket and arr[1,1]==zero_ket and arr[2,0]==zero_ket:
st.success('Computer has won !!')
flag=False
if not flag:
return 0
if flag:
#more 5 conditions
for index in [0,1,2]:
if(list(arr[index])==[one_ket,one_ket,one_ket]):
st.success("User has won !")
return 0
for index in [0,1,2]:
if(list(arr[index])==[zero_ket,zero_ket,zero_ket]):
st.success("Computer winss !")
return 0
for index in [0,1,2]:
if(list(arr[:,index])==[one_ket,one_ket,one_ket]):
st.success("User has won !")
return 0
for index in [0,1,2]:
if(list(arr[:,index])==[zero_ket,zero_ket,zero_ket]):
st.success("Computer winss !")
return 0
#for draw
if '|φ>' not in arr:
st.write("Its a DRAW...")
return 0
return 1
|