Skip to content

Commit d66a2c7

Browse files
committed
updated for API2 and did a little cleanup.
1 parent 1cf11ad commit d66a2c7

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

games/game_engines/quantum_slot/quantum_slot.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import threading
2+
import os
13
import json
24
from urllib.parse import urlencode
35
from urllib.request import urlopen
4-
from qiskit import *
5-
from qiskit.tools.monitor import job_monitor
6-
import ipywidgets as widgets
76
from IPython.display import display
8-
import threading
9-
import os
7+
import ipywidgets as widgets
8+
from qiskit import IBMQ, execute, QuantumCircuit, QuantumRegister, ClassicalRegister, BasicAer
9+
from qiskit.tools.monitor import job_monitor
10+
1011
script_dir = os.path.dirname(__file__)
1112

12-
IBMQ.load_accounts(hub=None)
13+
MY_PROVIDER = IBMQ.load_account()
14+
1315

1416
__all__ = ['quantum_slot_machine']
1517

@@ -101,7 +103,7 @@ def compute_payout(ints, qslot):
101103
elif all([x == 0 for x in ints]):
102104
value = 80
103105
# two bells
104-
elif sum([x == 0 for x in ints])== 2:
106+
elif sum([x == 0 for x in ints]) == 2:
105107
value = 5
106108
# two bells
107109
elif sum([x == 0 for x in ints]) == 1:
@@ -110,7 +112,7 @@ def compute_payout(ints, qslot):
110112
value = 0
111113
if value:
112114
update_credits(value, qslot)
113-
115+
114116
# if no credits left
115117
if qslot.children[0]._credits <= 0:
116118
qslot.children[1].children[1].value = front_str + \
@@ -131,11 +133,19 @@ def pull_slot(b):
131133
if alive:
132134
b.disabled = False
133135

136+
def choose_backend():
137+
from qiskit.providers.ibmq import least_busy
138+
large_enough_devices = MY_PROVIDER.backends(
139+
filters=lambda x: x.configuration().n_qubits >= 3
140+
and not x.configuration().simulator)
141+
return least_busy(large_enough_devices)
142+
134143
# generate new ibm q values
135144
def get_ibmq_ints(qslot):
136145
qslot.children[1].children[0].options = ['qasm_simulator', 'ANU QRNG']
137146
qslot.children[1].children[0].value = 'qasm_simulator'
138-
back = IBMQ.get_backend('ibmq_5_tenerife')
147+
# back = MY_PROVIDER.get_backend('ibmq_essex')
148+
back = choose_backend()
139149
q = QuantumRegister(3, name='q')
140150
c = ClassicalRegister(3, name='c')
141151
qc = QuantumCircuit(q, c)
@@ -310,6 +320,5 @@ def get_ibmq_ints(qslot):
310320

311321
opts = widgets.HBox(children=[solver, payout, out],
312322
layout=widgets.Layout(width='100%',
313-
justify_content='center',
314-
border='2px solid black'))
315-
323+
justify_content='center',
324+
border='2px solid black'))

games/quantum_slot_machine.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 2,
21+
"execution_count": 1,
2222
"metadata": {
2323
"ExecuteTime": {
2424
"end_time": "2018-12-28T15:54:29.168824Z",
@@ -30,7 +30,7 @@
3030
{
3131
"data": {
3232
"application/vnd.jupyter.widget-view+json": {
33-
"model_id": "d7b78a19e7d946c7b64c90cafb555fd1",
33+
"model_id": "a800b89078844bb399a15d512fda176a",
3434
"version_major": 2,
3535
"version_minor": 0
3636
},
@@ -82,8 +82,8 @@
8282
"Uses the local qasm simulator in Qiskit terra to pseudo-randomly generate bit strings from a single-shot of quantum circuit that consists of Hadamard gates on nine different qubits followed by measurements.\n",
8383
"\n",
8484
"\n",
85-
"* **ibmq_5_tenerife**\n",
86-
"Computes $300$ shots of a three-qubit variant of the same circuit used in the simulator. This is because only the `ibmq_5_tenerife` allows for accessing the `memory` of the backend, as opposed to computing the counts for all the shots. Although pre-computing random values is not ideal as an evil-doer may be able to read the numbers, on IBM Q devices, it is best to compute a lot of values at once and then cache them. After running once, this selection will allow you to use the machine $100$ times.\n",
85+
"* **Computation**\n",
86+
"Computes $300$ shots of a three-qubit variant of the same circuit used in the simulator. After running once, this selection will allow you to use the machine $100$ times.\n",
8787
"\n",
8888
"\n",
8989
"* **ANU QRNG**\n",
@@ -117,7 +117,7 @@
117117
"name": "python",
118118
"nbconvert_exporter": "python",
119119
"pygments_lexer": "ipython3",
120-
"version": "3.7.0"
120+
"version": "3.6.8"
121121
}
122122
},
123123
"nbformat": 4,

0 commit comments

Comments
 (0)