Skip to content

Commit f39b5f2

Browse files
authored
Merge pull request #15 from renier/update-battleship
Fix/update battleship game notebook
2 parents 6a2dbc6 + 2e0a705 commit f39b5f2

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

games/battleships_with_partial_NOT_gates.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"source": [
7878
"from qiskit import IBMQ\n",
7979
"from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit\n",
80-
"from qiskit import execute, register"
80+
"from qiskit import execute"
8181
]
8282
},
8383
{
@@ -95,8 +95,8 @@
9595
"metadata": {},
9696
"outputs": [],
9797
"source": [
98-
"# Load the saved IBMQ accounts\n",
99-
"IBMQ.load_accounts()"
98+
"# Load the saved IBMQ account\n",
99+
"IBMQ.load_account()"
100100
]
101101
},
102102
{
@@ -757,7 +757,7 @@
757757
"\n",
758758
" # compile and run the quantum program\n",
759759
" job = execute(qc, backend=device, shots=shots)\n",
760-
" if not device.configuration()['simulator']:\n",
760+
" if not device.configuration().to_dict()['simulator']:\n",
761761
" print(\"\\nWe'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\")\n",
762762
" else:\n",
763763
" print(\"\\nWe've now submitted the job to the simulator to see what happens to the ships of each player.\\n\")\n",

games/game_engines/battleships_engine.py

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from qiskit import Aer, IBMQ
1+
from qiskit import Aer, IBMQ, QuantumRegister, ClassicalRegister, QuantumCircuit, execute
22
import getpass, random, numpy, math
33

44
def title_screen ():
@@ -19,17 +19,82 @@ def title_screen ():
1919
print(" ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝╚═╝╚═╝ ╚══════╝")
2020
print("")
2121
print(" ___ ___ _ _ ")
22-
print(" | _ ) _ _ | \ ___ __ ___ __| | ___ | |__ _ _ ")
23-
print(" | _ \| || | | |) |/ -_)/ _|/ _ \/ _` |/ _ \| / /| || |")
24-
print(" |___/ \_, | |___/ \___|\__|\___/\__,_|\___/|_\_\ \_,_|")
22+
print(r' | _ ) _ _ | \ ___ __ ___ __| | ___ | |__ _ _ ')
23+
print(r' | _ \| || | | |) |/ -_)/ _|/ _ \/ _` |/ _ \| / /| || |')
24+
print(r' |___/ \_, | |___/ \___|\__|\___/\__,_|\___/|_\_\ \_,_|')
2525
print(" |__/ ")
2626
print("")
2727
print(" A game played on a real quantum computer!")
2828
print("")
2929
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("\nWe'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("\nWe'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)
3197

32-
3398
def ask_for_device ():
3499

35100
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 ):
155220

156221
print("Here is the percentage damage for ships that have been bombed.\n")
157222
print(display[ 4 ] + " " + display[ 0 ])
158-
print(" |\ /|")
159-
print(" | \ / |")
160-
print(" | \ / |")
223+
print(r' |\ /|')
224+
print(r' | \ / |')
225+
print(r' | \ / |')
161226
print(" | " + display[ 2 ] + " |")
162-
print(" | / \ |")
163-
print(" | / \ |")
164-
print(" |/ \|")
227+
print(r' | / \ |')
228+
print(r' | / \ |')
229+
print(r' |/ \|')
165230
print(display[ 3 ] + " " + display[ 1 ])
166231
print("\n")
167232
print("Ships with 95% damage or more have been destroyed\n")

0 commit comments

Comments
 (0)