Skip to content

Commit

Permalink
Merge pull request qiskit-community#56 from jwoehr/slot_machine_api2
Browse files Browse the repository at this point in the history
updated for API2 and did a little cleanup.
  • Loading branch information
Abraham Asfaw authored Jan 10, 2020
2 parents 0d2a449 + d66a2c7 commit c999b75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
33 changes: 21 additions & 12 deletions games/game_engines/quantum_slot/quantum_slot.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import threading
import os
import json
from urllib.parse import urlencode
from urllib.request import urlopen
from qiskit import *
from qiskit.tools.monitor import job_monitor
import ipywidgets as widgets
from IPython.display import display
import threading
import os
import ipywidgets as widgets
from qiskit import IBMQ, execute, QuantumCircuit, QuantumRegister, ClassicalRegister, BasicAer
from qiskit.tools.monitor import job_monitor

script_dir = os.path.dirname(__file__)

IBMQ.load_accounts(hub=None)
MY_PROVIDER = IBMQ.load_account()


__all__ = ['quantum_slot_machine']

Expand Down Expand Up @@ -101,7 +103,7 @@ def compute_payout(ints, qslot):
elif all([x == 0 for x in ints]):
value = 80
# two bells
elif sum([x == 0 for x in ints])== 2:
elif sum([x == 0 for x in ints]) == 2:
value = 5
# two bells
elif sum([x == 0 for x in ints]) == 1:
Expand All @@ -110,7 +112,7 @@ def compute_payout(ints, qslot):
value = 0
if value:
update_credits(value, qslot)

# if no credits left
if qslot.children[0]._credits <= 0:
qslot.children[1].children[1].value = front_str + \
Expand All @@ -131,11 +133,19 @@ def pull_slot(b):
if alive:
b.disabled = False

def choose_backend():
from qiskit.providers.ibmq import least_busy
large_enough_devices = MY_PROVIDER.backends(
filters=lambda x: x.configuration().n_qubits >= 3
and not x.configuration().simulator)
return least_busy(large_enough_devices)

# generate new ibm q values
def get_ibmq_ints(qslot):
qslot.children[1].children[0].options = ['qasm_simulator', 'ANU QRNG']
qslot.children[1].children[0].value = 'qasm_simulator'
back = IBMQ.get_backend('ibmq_5_tenerife')
# back = MY_PROVIDER.get_backend('ibmq_essex')
back = choose_backend()
q = QuantumRegister(3, name='q')
c = ClassicalRegister(3, name='c')
qc = QuantumCircuit(q, c)
Expand Down Expand Up @@ -310,6 +320,5 @@ def get_ibmq_ints(qslot):

opts = widgets.HBox(children=[solver, payout, out],
layout=widgets.Layout(width='100%',
justify_content='center',
border='2px solid black'))

justify_content='center',
border='2px solid black'))
10 changes: 5 additions & 5 deletions games/quantum_slot_machine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-12-28T15:54:29.168824Z",
Expand All @@ -30,7 +30,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d7b78a19e7d946c7b64c90cafb555fd1",
"model_id": "a800b89078844bb399a15d512fda176a",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -82,8 +82,8 @@
"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",
"\n",
"\n",
"* **ibmq_5_tenerife**\n",
"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",
"* **Computation**\n",
"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",
"\n",
"\n",
"* **ANU QRNG**\n",
Expand Down Expand Up @@ -117,7 +117,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.6.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit c999b75

Please sign in to comment.