Submit Ising-formulated problem and return samples.
(*, config_file, profile, endpoint, region, client_type, solver_def,
biases, couplings, random_problem, problem_size, num_reads, label,
sampling_params, verbose, json_output, output)
| 634 | @json_output |
| 635 | @standardized_output |
| 636 | def sample(*, config_file, profile, endpoint, region, client_type, solver_def, |
| 637 | biases, couplings, random_problem, problem_size, num_reads, label, |
| 638 | sampling_params, verbose, json_output, output): |
| 639 | """Submit Ising-formulated problem and return samples.""" |
| 640 | |
| 641 | # we'll limit max line len in non-verbose mode |
| 642 | maxlen = None if verbose else 120 |
| 643 | |
| 644 | # parse params (TODO: move to click validator) |
| 645 | params = {} |
| 646 | if sampling_params is not None: |
| 647 | try: |
| 648 | params = orjson.loads(sampling_params) |
| 649 | assert isinstance(params, dict) |
| 650 | except: |
| 651 | raise CLIError("sampling parameters required as JSON-encoded " |
| 652 | "map of param names to values", code=99) |
| 653 | |
| 654 | if num_reads is not None: |
| 655 | params.update(num_reads=num_reads) |
| 656 | |
| 657 | if label: |
| 658 | params.update(label=label) |
| 659 | |
| 660 | # TODO: add other params, like timeout? |
| 661 | config = dict( |
| 662 | config_file=config_file, profile=profile, |
| 663 | endpoint=endpoint, region=region, |
| 664 | client=client_type, solver=solver_def) |
| 665 | |
| 666 | t0 = timer() |
| 667 | client, solver = _get_client_solver(config, output) |
| 668 | |
| 669 | _, minimal_params = solver.minimal_problem |
| 670 | params = minimal_params | params |
| 671 | |
| 672 | if not isinstance(solver, (StructuredSolver, QuadraticUnstructuredSolverMixin)): |
| 673 | raise CLIError(f"Ising sampling not supported for solver: {solver!r}", code=99) |
| 674 | |
| 675 | if isinstance(solver, BaseUnstructuredSolver): |
| 676 | try: |
| 677 | import dimod |
| 678 | except ImportError: # pragma: no cover |
| 679 | raise RuntimeError("Can't sample from unstructured solver without dimod. " |
| 680 | "Re-install the library with 'bqm' support.") |
| 681 | |
| 682 | if random_problem: |
| 683 | if isinstance(solver, StructuredSolver): |
| 684 | linear, quadratic = generate_random_ising_problem(solver) |
| 685 | else: |
| 686 | bqm = dimod.generators.uniform(problem_size, 'SPIN') |
| 687 | linear, quadratic, _ = bqm.to_ising() |
| 688 | |
| 689 | else: |
| 690 | try: |
| 691 | linear = ast.literal_eval(biases) if biases else {} |
| 692 | if isinstance(linear, abc.Sequence): |
| 693 | linear = dict(enumerate(linear)) |