# Can you take classical code and compile it to quantum code?

Is there anyway for example, to take a trivial program from C such as:

int main() { int i = 1 + 1; return 0; } 

And compile this down to some form of Quantum Abstract High-level language, i.e. Quantum C?

From what I understand, quantum arithmetic is quite tricky business that uses QFTs. My first thought was to do the following for the example above:

Initialise two qubits and set the amplitudes like following: $$0|1|0|0$$

Where 0100 = 2 in decimal.

Somehow return this quantum register.

Using the amplitudes, you could perform 32-bit arithmetic with 32 qubits?

I haven't answered the notion of creating a function or returning either.

Essentially what I'm asking is the following, can we take known quantum methods (using Unitary functions to perform calculation) and apply this to classical code and if so, how?

EDIT: I've looked further and seen a few papers on Quantum Full Adders, I assume this is the way to go for addition? Is this too much? In quantum computation books that describe quantum assembly, there is not much mention of addition.

• Probably the best way to think of a quantum programming language is a classical programming language (e.g. C) with a new data type which represents quantum memory, and a bunch of new operations that work on quantum memory (e.g. Hadamard transform, controlled not, phase rotate). In that sense, every classical program is a quantum program which just doesn't use the quantum stuff. – Pseudonym Dec 9 '15 at 0:10
• Hi Pseudonym, I seem to understand everything barring arithmetic. I'm not sure how quantum gates can accomplish arithmetic? Or perhaps because I can just straight do addition using my qubits already? – watkinsr Dec 9 '15 at 0:13
• You first have to decide on what "quantum arithmetic" actually means. Once you've defined the precise operation that you want, then just like with classical logic, you can either implement the operator, or build it out of lower-level logic operators. In a sense, you don't really "need" integer addition in a classical programming language, either; you can build it out of logic operators if you feel so inclined. – Pseudonym Dec 9 '15 at 0:59
• So essentially one could just suppose that hardware will take care of it and write it how I see fit? For instance, if we take addition, I could just suppose that hardware quantum full adders will exist and my quantum programming language could have for instance: int j = 1 + 2 ? I think the problem is I never really understood how classical assembly understands such things. – watkinsr Dec 9 '15 at 1:15

## 1 Answer

Doubly-controlled NOT gates are universal with respect to classical functions. Quantum computers can perform doubly-controlled NOTs. So even a quantum computer not allowed to perform fancy operations involving superposition, and not allowed to perform irreversible operations such as measuring or clearing a bit, can perform any classical computation.

For example, a quantum computer can basically directly run the gates making up a VanRentergem addition circuit: Quantum computers are actually slightly better at reversible computation than classical computers. Classically you sometimes need an extra scratch bit to compute a function, because of permutation parity issues, but quantum shenanigans get that down to zero. Though of course emulating irreversible functions still requires introducing new scratch bits each time.

• Hi Strilanc, This stackoverflow post addresses making a NAND gate with Toffoli gates but I'm confused. A NAND takes 2 inputs and produces one output, where-as a toffoli takes 3 and produces 3. While the gate seems to do what the NAND does (by changing the arbitrary 1 to a zero, if a & b are set, I'm confused because this would mean only the changed zero is my output?Perhaps you could provide me with an explanation on how to form NAND gates from Toffoli gates? – watkinsr Dec 9 '15 at 23:30