MCPcopy Create free account
hub / github.com/dwavesystems/dwave-cloud-client / reformat_parameters

Method reformat_parameters

dwave/cloud/solver.py:1481–1545  ·  view source on GitHub ↗

Reformat some solver parameters for SAPI. Currently the only reformatted parameter is ``initial_state``. This method allows ``initial_state`` to be submitted as a dictionary mapping the qubits to their initial value. Args: vartype: One of ``'ising'`` or

(vartype: '_Vartype',
                            parameters: abc.MutableMapping[str, Any],
                            properties: abc.Mapping[str, Any],
                            inplace: bool = False,
                            )

Source from the content-addressed store, hash-verified

1479
1480 @staticmethod
1481 def reformat_parameters(vartype: '_Vartype',
1482 parameters: abc.MutableMapping[str, Any],
1483 properties: abc.Mapping[str, Any],
1484 inplace: bool = False,
1485 ) -> abc.MutableMapping[str, Any]:
1486 """Reformat some solver parameters for SAPI.
1487
1488 Currently the only reformatted parameter is ``initial_state``. This
1489 method allows ``initial_state`` to be submitted as a dictionary
1490 mapping the qubits to their initial value.
1491
1492 Args:
1493 vartype: One of ``'ising'`` or ``'qubo'``. If :mod:`dimod` is
1494 installed, this can also be any
1495 :class:`~dimod.typing.VartypeLike`.
1496 parameters: The parameters to submit to this solver.
1497 properties: The solver's properties. Either
1498 :attr:`StructuredSolver.properties` or
1499 :attr:`dwave.systems.DWaveSampler.properties` can be
1500 provided.
1501 inplace: Whether to modify the ``parameters`` in-place or return
1502 a copy.
1503
1504 Returns:
1505 The reformatted solver parameters.
1506 If ``inplace`` is true the modified ``parameters`` is returned.
1507 If ``inplace`` is false then a deep copy of ``parameters``
1508 with the relevant fields updated is returned.
1509
1510 """
1511 # whether to copy or not
1512 parameters = parameters if inplace else copy.deepcopy(parameters)
1513
1514 # handle the vartype
1515 if vartype not in ('ising', 'qubo'):
1516 try:
1517 vartype = 'ising' if dimod.as_vartype(vartype) is dimod.SPIN else 'qubo'
1518 except (TypeError, AttributeError):
1519 msg = "expected vartype to be one of: 'ising', 'qubo'"
1520 if dimod:
1521 msg += ", 'BINARY', 'SPIN', dimod.BINARY, dimod.SPIN"
1522 msg += f"; {vartype!r} provided"
1523 raise ValueError(msg) from None
1524
1525 # update the parameters
1526 if 'initial_state' in parameters:
1527 initial_state = parameters['initial_state']
1528 if isinstance(initial_state, abc.Mapping):
1529
1530 initial_state_list = [3]*properties['num_qubits']
1531
1532 low = -1 if vartype == 'ising' else 0
1533
1534 for v, val in initial_state.items():
1535 if val == 3:
1536 continue
1537 if val <= 0:
1538 initial_state_list[v] = low

Callers 7

_format_paramsMethod · 0.95
test_emptyMethod · 0.80
test_initial_statesMethod · 0.80
test_vartype_smokeMethod · 0.80

Calls

no outgoing calls

Tested by 6

test_emptyMethod · 0.64
test_initial_statesMethod · 0.64
test_vartype_smokeMethod · 0.64