Is a² — b² the same as (a+b)*(a-b)?

Yuval Boger

Mathematically, the answer is yes, of course, they are the same because:

a² — b² = a*a-b*b = (a+b)*(a-b)

But if you are doing quantum arithmetic, while the output might be the same, it is advisable to consider whether calculating a² — b² or calculating (a+b)*(a-b) is better.

Why do we care about quantum arithmetic? Arithmetic and logic are an inherent part of some of the most useful quantum algorithms, such as creating complex oracles in a Grover search, embedding classical wisdom in a VQE ansatz, or comparing asset price to a benchmark when calculating option pricing using the amplitude estimation algorithm.

Therefore, to implement useful algorithms, it is often necessary to implement arithmetic functions.

Implementing a² — b² and (a+b)(a-b)

Calculating a*a-b*b would require two multiplications — multiple a by itself and then multiple b by itself) — followed by subtracting b*b from a*a. It might look something like this[1]:

Calculating (a+b)*(a-b) would require an addition, a subtraction, and then a multiplication. Let’s assume we prefer this approach. But even here, we could implement it using a different number of ancilla qubits. For instance, here is one implementation:

And here is another implementation:

Selecting the number of ancilla bits (or, in other words, the total number of qubits the circuit uses) might have an impact on the depth of the circuit. Based on what we know about the hardware — available qubits, accuracy, etc. — we might prefer one over the other.

There might be additional system-wide considerations: for instance, the first two implementations preserve the value of a so that it can be used later in the circuit. The third implementation does not preserve the value of a.

Resource Estimation

If I’m a quantum software engineer tasked with creating a circuit that implements this arithmetic, I would love to be able to estimate the number of qubits and the depth of the circuit for each of the four cases:

  1. a² — b² with fewer ancilla bits
  2. a² — b² with more ancilla bits
  3. (a+b)*(a-b) with fewer ancilla bits
  4. (a+b)*(a-b) with more ancilla bits

If I don’t estimate, I might spend a lot of time implementing this particular function only to discover that I have to restart because I’ve exceeded the constraints of the hardware or other goals that I have.

Fortunately, with the Classiq platform, this is very simple. By using the Classiq modeling language, I’m able to quickly change between the various options. For instance, for the first two versions, I might create a model with the following code:

These would generate two completely different circuits, each meeting the constraints:


Quantum arithmetic is important, and not as easy as it seems at first blush.

While (a+b)*(a-b) seems simple, what if you had to build a circuit that implements

These quickly become impossible to design at the gate level, necessitating an advanced platform like Classiq’s.

The Classiq platform provides an efficient method of designing a circuit that implements this arithmetic, as well as estimating the required resources.

[1] the ‘/’ sign on each line denotes that the a and the b registers might be deeper than one qubit

See Also

No items found.

Start Creating Quantum Software Without Limits

contact us