def multiplication_circuit(nbit, vartype=dimod.BINARY):
if nbit < <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>num_multiplier_bits = num_multiplicand_bits = nbit<EOL>csp = ConstraintSatisfactionProblem(vartype)<EOL>a = {i: '<STR_LIT>' % i for i in range(nbit)}<EOL>b = {j: '<STR_LIT>' % j for j in range(nbit)}<EOL>p = {k: '<STR_LIT>' % k for k in range(nbit + nbit)}<EOL>AND = defaultdict(dict)  <EOL>SUM = defaultdict(dict)  <EOL>CARRY = defaultdict(dict)  <EOL>for i in range(num_multiplier_bits):<EOL><INDENT>for j in range(num_multiplicand_bits):<EOL><INDENT>ai = a[i]<EOL>bj = b[j]<EOL>if i == <NUM_LIT:0> and j == <NUM_LIT:0>:<EOL><INDENT>andij = AND[i][j] = p[<NUM_LIT:0>]<EOL>gate = and_gate([ai, bj, andij], vartype=vartype, name='<STR_LIT>' % (ai, bj, andij))<EOL>csp.add_constraint(gate)<EOL>continue<EOL><DEDENT>andij = AND[i][j] = '<STR_LIT>' % (i, j)<EOL>gate = and_gate([ai, bj, andij], vartype=vartype, name='<STR_LIT>' % (ai, bj, andij))<EOL>csp.add_constraint(gate)<EOL>inputs = [andij]<EOL>if i - <NUM_LIT:1> in CARRY and j in CARRY[i - <NUM_LIT:1>]:<EOL><INDENT>inputs.append(CARRY[i - <NUM_LIT:1>][j])<EOL><DEDENT>if i - <NUM_LIT:1> in SUM and j + <NUM_LIT:1> in SUM[i - <NUM_LIT:1>]:<EOL><INDENT>inputs.append(SUM[i - <NUM_LIT:1>][j + <NUM_LIT:1>])<EOL><DEDENT>if len(inputs) == <NUM_LIT:1>:<EOL><INDENT>SUM[i][j] = andij<EOL><DEDENT>elif len(inputs) == <NUM_LIT:2>:<EOL><INDENT>if j == <NUM_LIT:0>:<EOL><INDENT>sumij = SUM[i][j] = p[i]<EOL><DEDENT>else:<EOL><INDENT>sumij = SUM[i][j] = '<STR_LIT>' % (i, j)<EOL><DEDENT>carryij = CARRY[i][j] = '<STR_LIT>' % (i, j)<EOL>name = '<STR_LIT>' % (inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], sumij, carryij)<EOL>gate = halfadder_gate([inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], sumij, carryij], vartype=vartype, name=name)<EOL>csp.add_constraint(gate)<EOL><DEDENT>else:<EOL><INDENT>assert len(inputs) == <NUM_LIT:3>, '<STR_LIT>'<EOL>if j == <NUM_LIT:0>:<EOL><INDENT>sumij = SUM[i][j] = p[i]<EOL><DEDENT>else:<EOL><INDENT>sumij = SUM[i][j] = '<STR_LIT>' % (i, j)<EOL><DEDENT>carryij = CARRY[i][j] = '<STR_LIT>' % (i, j)<EOL>name = '<STR_LIT>' % (inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], inputs[<NUM_LIT:2>], sumij, carryij)<EOL>gate = fulladder_gate([inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], inputs[<NUM_LIT:2>], sumij, carryij], vartype=vartype, name=name)<EOL>csp.add_constraint(gate)<EOL><DEDENT><DEDENT><DEDENT>for col in range(nbit - <NUM_LIT:1>):<EOL><INDENT>inputs = [CARRY[nbit - <NUM_LIT:1>][col], SUM[nbit - <NUM_LIT:1>][col + <NUM_LIT:1>]]<EOL>if col == <NUM_LIT:0>:<EOL><INDENT>sumout = p[nbit + col]<EOL>carryout = CARRY[nbit][col] = '<STR_LIT>' % (nbit, col)<EOL>name = '<STR_LIT>' % (inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], sumout, carryout)<EOL>gate = halfadder_gate([inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], sumout, carryout], vartype=vartype, name=name)<EOL>csp.add_constraint(gate)<EOL>continue<EOL><DEDENT>inputs.append(CARRY[nbit][col - <NUM_LIT:1>])<EOL>sumout = p[nbit + col]<EOL>if col < nbit - <NUM_LIT:2>:<EOL><INDENT>carryout = CARRY[nbit][col] = '<STR_LIT>' % (nbit, col)<EOL><DEDENT>else:<EOL><INDENT>carryout = p[<NUM_LIT:2> * nbit - <NUM_LIT:1>]<EOL><DEDENT>name = '<STR_LIT>' % (inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], inputs[<NUM_LIT:2>], sumout, carryout)<EOL>gate = fulladder_gate([inputs[<NUM_LIT:0>], inputs[<NUM_LIT:1>], inputs[<NUM_LIT:2>], sumout, carryout], vartype=vartype, name=name)<EOL>csp.add_constraint(gate)<EOL><DEDENT>return csp<EOL>
Multiplication circuit constraint satisfaction problem.

    A constraint satisfaction problem that represents the binary multiplication :math:`ab=p`,
    where the multiplicands are binary variables of length `nbit`; for example,
    :math:`a_0 + 2a_1 + 4a_2 +... +2^ma_{nbit}`.

    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     |
      --------------------------------------------------------------------------------

    Args:
        nbit (int): Number of bits in the multiplicands.
        vartype (Vartype, optional, default='BINARY'): Variable type. Accepted
            input values:

            * Vartype.SPIN, 'SPIN', {-1, 1}
            * Vartype.BINARY, 'BINARY', {0, 1}

    Returns:
        CSP (:obj:`.ConstraintSatisfactionProblem`): CSP that is satisfied when variables
        :math:`a,b,p` are assigned values that correctly solve binary multiplication :math:`ab=p`.

    Examples:
        This example creates a multiplication circuit CSP that multiplies two 3-bit numbers,
        which is then formulated as a binary quadratic model (BQM). It fixes the multiplacands
        as :math:`a=5, b=6` (:math:`101` and :math:`110`) and uses a simulated annealing sampler
        to find the product, :math:`p=30` (:math:`111100`).

        >>> import dwavebinarycsp
        >>> from dwavebinarycsp.factories.csp.circuits import multiplication_circuit
        >>> import neal
        >>> csp = multiplication_circuit(3)
        >>> bqm = dwavebinarycsp.stitch(csp)
        >>> bqm.fix_variable('a0', 1); bqm.fix_variable('a1', 0); bqm.fix_variable('a2', 1)
        >>> bqm.fix_variable('b0', 1); bqm.fix_variable('b1', 1); bqm.fix_variable('b2', 0)
        >>> sampler = neal.SimulatedAnnealingSampler()
        >>> response = sampler.sample(bqm)
        >>> p = next(response.samples(n=1, sorted_by='energy'))
        >>> print(p['p0'], p['p1'], p['p2'], p['p3'], p['p4'], p['p5'])    # doctest: +SKIP
        1 1 1 1 0 0