Skip to content

Commit 3e44e3f

Browse files
committed
Add --label option to dwave ping/sample (with a default)
Close #481.
1 parent e87252b commit 3e44e3f

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

dwave/cloud/cli.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,13 @@ def flush():
441441
help='Connection and read timeouts (in seconds) for all API requests')
442442
@click.option('--polling-timeout', default=None, type=float,
443443
help='Problem polling timeout in seconds (time-to-solution timeout)')
444+
@click.option('--label', default='dwave ping', type=str, help='Problem label')
444445
@click.option('--json', 'json_output', default=False, is_flag=True,
445446
help='JSON output')
446447
@standardized_output
447448
def ping(*, config_file, profile, endpoint, region, client_type, solver_def,
448-
sampling_params, json_output, request_timeout, polling_timeout, output):
449+
sampling_params, request_timeout, polling_timeout, label, json_output,
450+
output):
449451
"""Ping the QPU by submitting a single-qubit problem."""
450452

451453
# parse params (TODO: move to click validator)
@@ -458,6 +460,9 @@ def ping(*, config_file, profile, endpoint, region, client_type, solver_def,
458460
raise CLIError("sampling parameters required as JSON-encoded "
459461
"map of param names to values", code=99)
460462

463+
if label:
464+
params.update(label=label)
465+
461466
config = dict(
462467
config_file=config_file, profile=profile,
463468
endpoint=endpoint, region=region,
@@ -555,6 +560,7 @@ def solvers(config_file, profile, endpoint, region, client_type, solver_def,
555560
help='Submit a valid random problem using all qubits')
556561
@click.option('--num-reads', '-n', default=None, type=int,
557562
help='Number of reads/samples')
563+
@click.option('--label', default='dwave sample', type=str, help='Problem label')
558564
@click.option('--sampling-params', '-m', default=None,
559565
help='Sampling parameters, JSON encoded')
560566
@click.option('--verbose', '-v', default=False, is_flag=True,
@@ -563,7 +569,7 @@ def solvers(config_file, profile, endpoint, region, client_type, solver_def,
563569
help='JSON output')
564570
@standardized_output
565571
def sample(*, config_file, profile, endpoint, region, client_type, solver_def,
566-
biases, couplings, random_problem, num_reads, sampling_params,
572+
biases, couplings, random_problem, num_reads, label, sampling_params,
567573
verbose, json_output, output):
568574
"""Submit Ising-formulated problem and return samples."""
569575

@@ -583,6 +589,9 @@ def sample(*, config_file, profile, endpoint, region, client_type, solver_def,
583589
if num_reads is not None:
584590
params.update(num_reads=num_reads)
585591

592+
if label:
593+
params.update(label=label)
594+
586595
# TODO: add other params, like timeout?
587596
config = dict(
588597
config_file=config_file, profile=profile,

releasenotes/notes/cli-tweaks-687abaa266fe3a72.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ features:
55
- |
66
Add ``--sampling-params`` option to ``dwave sample``. ``--num-reads`` has
77
been kept, and it now overwrites value given in sampling params.
8+
- |
9+
Add ``--label`` option to ``dwave ping`` and ``dwave sample``, with a
10+
default value set. See `#481 <https://github.com/dwavesystems/dwave-cloud-client/issues/481>`_.
811
fixes:
912
- |
1013
Improved error and output handling in ``dwave sample``.

tests/test_cli.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ def test_ping(self, config_file_option):
247247
profile = 'profile'
248248
client_type = 'qpu'
249249
params = dict(num_reads=10)
250+
label = 'label'
250251

251252
with mock.patch('dwave.cloud.cli.Client') as m:
252253
# mock returned solver
@@ -262,7 +263,8 @@ def test_ping(self, config_file_option):
262263
'--client', client_type,
263264
'--sampling-params', json.dumps(params),
264265
'--request-timeout', '.5',
265-
'--polling-timeout', '30'])
266+
'--polling-timeout', '30',
267+
'--label', label])
266268

267269
# proper arguments passed to Client.from_config?
268270
m.from_config.assert_called_with(
@@ -276,7 +278,8 @@ def test_ping(self, config_file_option):
276278

277279
# sampling method called on solver with correct params?
278280
solver = client.get_solver.return_value
279-
solver.sample_ising.assert_called_with({3: 0}, {}, **params)
281+
solver.sample_ising.assert_called_with(
282+
{3: 0}, {}, label=label, **params)
280283

281284
self.assertEqual(result.exit_code, 0)
282285

@@ -293,6 +296,7 @@ def test_sample(self, config_file_option):
293296
biases = '[0]'
294297
couplings = '{(0, 4): 1}'
295298
num_reads = '10'
299+
label = 'label'
296300

297301
with mock.patch('dwave.cloud.cli.Client') as m:
298302

@@ -304,6 +308,7 @@ def test_sample(self, config_file_option):
304308
'--profile', profile,
305309
'--client', client,
306310
'--solver', solver,
311+
'--label', label,
307312
'-h', biases,
308313
'-j', couplings,
309314
'-n', num_reads])
@@ -320,7 +325,8 @@ def test_sample(self, config_file_option):
320325

321326
# sampling method called on solver?
322327
s = c.get_solver.return_value
323-
s.sample_ising.assert_called_with({0: 0}, {(0, 4): 1}, num_reads=10)
328+
s.sample_ising.assert_called_with(
329+
{0: 0}, {(0, 4): 1}, num_reads=10, label=label)
324330

325331
self.assertEqual(result.exit_code, 0)
326332

0 commit comments

Comments
 (0)