# Implementing piecewise functions on a quantum computer

I am curious about how to do implement functions like $$f(x)=\begin{cases} 2x &\text{if} &0\leq x <0.5 \\ x/2 &\text{if} &0.5\leq x<1 \end{cases}$$ Do we implement this like any other equation on a classical computer? What initial states and assumptions do we need to make for this computation? What gates do we need to use?

• You might want to read through arXiv1805.12445. Of course, it doesn't discuss the specific kind of piecewise function you're talking about, but with a little ingenuity, you should be able to get there. May 13 '19 at 7:04

First, you need to check if your function is reversible. This determines whether you can perform it inline or not. Your function is not reversible, so we need to perform something like $$x, y \rightarrow x, y + f(x)$$ instead of $$x \rightarrow f(x)$$.

The great thing about functions of the form $$x, y \rightarrow x, y + f(x)$$ is that it's very easy to derive a quantum circuit for them from a classical circuit. You just:

1. Compile the function down to a series of assembly instructions, ideally without any jumps. You could use a traditional compiler for this.

2. Compile each assembly instruction into a classic logic circuit.

3. Switch to the reversible gate set by introducing work space. Replace AND gates with Toffoli gates onto fresh zero qubits, and etc.

4. Add the result register into $$y$$ using a standard addition circuit.

5. Uncompute all the junk data that was computed during step 3.

Of course in general you'll want to try to be a bit clever about it in order to create an efficient circuit.