liveBook · Manning

Two's complement

As explained in Building Quantum Software with Python

Two's complement is a signed binary representation in which the leading bit distinguishes nonnegative values from negative values and an m-bit string represents values from −2^(m−1) through 2^(m−1)−1.

Two's complement represents signed integers in a fixed-width binary register. The leading bit distinguishes nonnegative values from negative values: an m-bit string covers −2^(m−1) through 2^(m−1)−1. section 10.1.5

In quantum function encoding, two's complement provides a practical interpretation for value qubits:

  • Strings beginning with 0 represent nonnegative outputs. section 10.1.5
  • Strings beginning with 1 represent negative outputs. section 10.1.5
  • With four value qubits, the representable range is −8 through 7. section 11.1
  • A grid-state visualization can display negative values by setting neg=True. section 10.1.5

Encoding f(k) = k² − 3

For inputs 0 through 3, the lowest output is −3 and the highest is 0. The output −3 is encoded as 1101 in a four-bit value register; the function's grid shape remains the same but shifts to include negative values. section 10.1.5

Figure 10.10 The quantum state encoding the function f(k) = k2 – 3 for 0 ≤ k < 4
figure

View the quantum-state figure for f(k) = k² − 3 and its two's complement output encoding. section 10.1.5

How Building Quantum Software with Python covers it

How deep it goes
Defines sign-bit ranges and demonstrates four-bit encodings, but does not derive two's complement arithmetic.
Examples
Uses f(k)=k²−3 with output 1101 for −3 and f(k)=k²−5 with a four-qubit range from −8 through 7.
What it assumes
Assumes binary input variables and quantum registers while supplying the signed interpretation needed for negative outputs.
What you can do after
Apply it via build_polynomial_circuit, grid_state, and oracle_match_0 to encode and search signed quantum function outputs.
How it connects
Connects binary variables, quantum states, value registers, function encoding, oracle construction, and Grover adaptive search.

Also explained in

Compare how 4 books explain two's complement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Quantum Software with Python. The section links point to the parts of the book it draws on.