dimod.generators.multiplication_circuit#

multiplication_circuit(num_arg1_bits: int, num_arg2_bits: int | None = None) BinaryQuadraticModel[source]#

Generate a binary quadratic model with ground states corresponding to a multiplication circuit.

The generated BQM represents the binary multiplication \(ab=p\), where the args are binary variables of length num_arg1_bits and num_arg2_bits; for example, \(2^ma_{num_arg1_bits} + ... + 4a_2 + 2a_1 + a0\). The square below shows a graphic representation of the circuit:

________________________________________________________________________________
|                                         and20         and10         and00    |
|                                           |             |             |      |
|                           and21         add11──and11  add01──and01    |      |
|                             |┌───────────┘|┌───────────┘|             |      |
|             and22         add12──and12  add02──and02    |             |      |
|               |┌───────────┘|┌───────────┘|             |             |      |
|             add13─────────add03           |             |             |      |
|  ┌───────────┘|             |             |             |             |      |
| p5            p4            p3            p2            p1            p0     |
--------------------------------------------------------------------------------
Parameters:
  • num_arg1_bits – Number of bits in the first argument.

  • num_arg2_bits – Number of bits in the second argument. If None, set to num_arg1_bits.

Returns:

A binary quadratic model with ground states corresponding to a multiplication circuit.

Examples

This example creates a multiplication circuit BQM that multiplies two 2-bit numbers. It fixes the multiplacands as \(a=2, b=3\) (\(10\) and \(11\)) and uses a brute-force solver to find the product, \(p=6\) (\(110\)).

>>> from dimod.generators import multiplication_circuit
>>> from dimod import ExactSolver
>>> bqm = multiplication_circuit(2)
>>> for fixed_var, fixed_val in {'a0': 0, 'a1': 1, 'b0':1, 'b1': 1}.items():
...    bqm.fix_variable(fixed_var, fixed_val)
>>> best = ExactSolver().sample(bqm).first
>>> p = {key: best.sample[key] for key in best.sample.keys() if "p" in key}
>>> print(p)
{'p0': 0, 'p1': 1, 'p2': 1, 'p3': 0}