1
- from qiskit import Aer , IBMQ
1
+ from qiskit import Aer , IBMQ , QuantumRegister , ClassicalRegister , QuantumCircuit , execute
2
2
import getpass , random , numpy , math
3
3
4
4
def title_screen ():
@@ -19,17 +19,82 @@ def title_screen ():
19
19
print (" ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ╚══════╝" )
20
20
print ("" )
21
21
print (" ___ ___ _ _ " )
22
- print (" | _ ) _ _ | \ ___ __ ___ __| | ___ | |__ _ _ " )
23
- print (" | _ \| || | | |) |/ -_)/ _|/ _ \/ _` |/ _ \| / /| || |" )
24
- print (" |___/ \_, | |___/ \___|\__|\___/\__,_|\___/|_\_\ \_,_|" )
22
+ print (r' | _ ) _ _ | \ ___ __ ___ __| | ___ | |__ _ _ ' )
23
+ print (r' | _ \| || | | |) |/ -_)/ _|/ _ \/ _` |/ _ \| / /| || |' )
24
+ print (r' |___/ \_, | |___/ \___|\__|\___/\__,_|\___/|_\_\ \_,_|' )
25
25
print (" |__/ " )
26
26
print ("" )
27
27
print (" A game played on a real quantum computer!" )
28
28
print ("" )
29
29
print ("" )
30
- randPlace = input ("> Press Enter to play...\n " ).upper ()
30
+ input ("> Press Enter to play...\n " ).upper ()
31
+
32
+
33
+ def play_game ():
34
+ # the game variable will be set to False once the game is over
35
+ game = True
36
+
37
+ # the variable bombs[X][Y] will hold the number of times position Y has been bombed by player X+1
38
+ bomb = [[0 ]* 5 for _ in range (2 )] # all values are initialized to zero
39
+
40
+ # set the number of samples used for statistics
41
+ shots = 1024
42
+
43
+ # the variable grid[player] will hold the results for the grid of each player
44
+ grid = [{}, {}]
45
+
46
+ # ask what kind of quantum device will be used (real or simulated)
47
+ device = ask_for_device ()
48
+
49
+ # ask players where thir ships are
50
+ shipPos = ask_for_ships ()
51
+
52
+ while (game ):
53
+
54
+ # ask both players where they want to bomb, and update the list of bombings so far
55
+ bomb = ask_for_bombs (bomb )
56
+
57
+ # now we create and run the quantum programs that implement this on the grid for each player
58
+ qc = []
59
+ for player in range (2 ):
60
+
61
+ # now to set up the quantum program to simulate the grid for this player
62
+
63
+ # set up registers and program
64
+ q = QuantumRegister (5 )
65
+ c = ClassicalRegister (5 )
66
+ qc .append (QuantumCircuit (q , c ))
67
+
68
+ # add the bombs (of the opposing player)
69
+ for position in range (5 ):
70
+ # add as many bombs as have been placed at this position
71
+ for _ in range (bomb [(player + 1 )% 2 ][position ]):
72
+ # the effectiveness of the bomb
73
+ # (which means the quantum operation we apply)
74
+ # depends on which ship it is
75
+ for ship in [0 ,1 ,2 ]:
76
+ if (position == shipPos [player ][ship ]):
77
+ frac = 1 / (ship + 1 )
78
+ # add this fraction of a NOT to the QASM
79
+ qc [player ].u3 (frac * math .pi , 0.0 , 0.0 , q [position ])
80
+
81
+ # Finally, measure them
82
+ for position in range (5 ):
83
+ qc [player ].measure (q [position ], c [position ])
84
+
85
+ # compile and run the quantum program
86
+ job = execute (qc , backend = device , shots = shots )
87
+ if not device .configuration ().to_dict ()['simulator' ]:
88
+ print ("\n We've now submitted the job to the quantum computer to see what happens to the ships of each player\n (it might take a while).\n " )
89
+ else :
90
+ print ("\n We've now submitted the job to the simulator to see what happens to the ships of each player.\n " )
91
+ # and extract data
92
+ for player in range (2 ):
93
+ grid [player ] = job .result ().get_counts (qc [player ])
94
+ print (grid )
95
+
96
+ game = display_grid (grid , shipPos , shots )
31
97
32
-
33
98
def ask_for_device ():
34
99
35
100
d = input ("Do you want to play on the real device? (y/n)\n " ).upper ()
@@ -155,13 +220,13 @@ def display_grid ( grid, shipPos, shots ):
155
220
156
221
print ("Here is the percentage damage for ships that have been bombed.\n " )
157
222
print (display [ 4 ] + " " + display [ 0 ])
158
- print (" |\ /|" )
159
- print (" | \ / |" )
160
- print (" | \ / |" )
223
+ print (r' |\ /|' )
224
+ print (r' | \ / |' )
225
+ print (r' | \ / |' )
161
226
print (" | " + display [ 2 ] + " |" )
162
- print (" | / \ |" )
163
- print (" | / \ |" )
164
- print (" |/ \|" )
227
+ print (r' | / \ |' )
228
+ print (r' | / \ |' )
229
+ print (r' |/ \|' )
165
230
print (display [ 3 ] + " " + display [ 1 ])
166
231
print ("\n " )
167
232
print ("Ships with 95% damage or more have been destroyed\n " )
0 commit comments