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

Function encode_problem_as_qp

dwave/cloud/coders.py:50–119  ·  view source on GitHub ↗

Encode the binary quadratic problem for submission to a given solver, using the `qp` format for data. Args: solver (:class:`dwave.cloud.solver.Solver`): The solver used. linear (dict[variable, bias]/list[variable, bias]): Linear terms of the model.

(solver: 'dwave.cloud.solver.StructuredSolver',
                         linear: Union[list[float], dict[int, float]],
                         quadratic: dict[tuple[int, int], float],
                         offset: float = 0,
                         undirected_biases: bool = False
                         )

Source from the content-addressed store, hash-verified

48
49
50def encode_problem_as_qp(solver: 'dwave.cloud.solver.StructuredSolver',
51 linear: Union[list[float], dict[int, float]],
52 quadratic: dict[tuple[int, int], float],
53 offset: float = 0,
54 undirected_biases: bool = False
55 ) -> EncodedQP:
56 """Encode the binary quadratic problem for submission to a given solver,
57 using the `qp` format for data.
58
59 Args:
60 solver (:class:`dwave.cloud.solver.Solver`):
61 The solver used.
62
63 linear (dict[variable, bias]/list[variable, bias]):
64 Linear terms of the model.
65
66 quadratic (dict[(variable, variable), bias]):
67 Quadratic terms of the model.
68
69 offset (number, default=0):
70 Constant offset applied to the model.
71
72 undirected_biases (boolean, default=False):
73 Are (quadratic) biases specified on undirected edges?
74
75 Returns:
76 Encoded submission dictionary.
77 """
78 # convert legacy format (list) to dict for performance
79 if isinstance(linear, abc.Sequence):
80 linear = dict(enumerate(linear))
81
82 active = active_qubits(linear, quadratic)
83
84 # Encode linear terms. The coefficients of the linear terms of the objective
85 # are encoded as an array of little endian 64 bit doubles.
86 # This array is then base64 encoded into a string safe for json.
87 # The order of the terms is determined by the _encoding_qubits property
88 # specified by the server.
89 # Note: only active qubits are coded with double, inactive with NaN
90 nan = float('nan')
91 lin = [linear.get(qubit, 0 if qubit in active else nan)
92 for qubit in solver._encoding_qubits]
93
94 lin = base64.b64encode(struct.pack('<' + ('d' * len(lin)), *lin))
95
96 # Encode the coefficients of the quadratic terms of the objective
97 # in the same manner as the linear terms, in the order given by the
98 # _encoding_couplers property, discarding tailing zero couplings
99 if undirected_biases:
100 # quadratic biases are given in a triangular or symmetric matrix
101 quad = [quadratic.get((q1,q2), quadratic.get((q2,q1), 0))
102 for (q1,q2) in solver._encoding_couplers
103 if q1 in active and q2 in active]
104 else:
105 # quadratic biases are defined on directed edges, conflate with sum
106 quad = [quadratic.get((q1,q2), 0) + quadratic.get((q2,q1), 0)
107 for (q1,q2) in solver._encoding_couplers

Calls 2

active_qubitsFunction · 0.90
getMethod · 0.45