# Compute the output of a quantum circuit with classically controlled operations

Consider the following circuit :

where $$|\psi\rangle$$ is a qubit in $$\mathbb{C}^2$$, $$|0\rangle= \begin{pmatrix}1 \\ 0 \end{pmatrix}$$, $$T= \begin{pmatrix}1 & 0\\ 0 & e^{i\pi/4} \end{pmatrix}$$ is the $$\pi/8$$ gate, $$H= \frac{1}{\sqrt{2}}\begin{pmatrix}1 & 1\\ 1 & -1 \end{pmatrix}$$ is the Hadamard gate, $$X= \begin{pmatrix}0 & 1\\ 1 & 0 \end{pmatrix}$$ and $$P= \begin{pmatrix}1 & 0\\ 0 & i \end{pmatrix}$$.

In this circuit, the $$PX$$ gate is only applied when the measurement of the top wire outcome is $$1$$. This measurement is done with respect to the computational basis $$|0\rangle, |1\rangle$$.

The task is to verify the resulting state from this circuit, but it doesn't seem clear how we can retrieve $$T|\psi\rangle$$ exactly.

Note that: $$TH|0\rangle= \frac{1}{\sqrt{2}}T\begin{pmatrix}1\\ 1 \end{pmatrix}= \frac{1}{\sqrt{2}}\begin{pmatrix}1\\ e^{i\pi/4} \end{pmatrix}$$ then after the CNOT gate:

\begin{align} \text{CNOT}(TH|0\rangle|\psi\rangle)&= \begin{pmatrix}1 & 0& 0& 0 \\ 0 & 1& 0& 0 \\ 0 & 0& 0& 1 \\ 0 & 0& 1& 0 \\ \end{pmatrix}\frac{1}{\sqrt{2}}\begin{pmatrix}1\\ e^{i\pi/4} \end{pmatrix}\otimes \begin{pmatrix}\psi_0\\ \psi_1 \end{pmatrix}\\ &= \frac{1}{\sqrt{2}}\begin{pmatrix}1 & 0& 0& 0 \\ 0 & 1& 0& 0 \\ 0 & 0& 0& 1 \\ 0 & 0& 1& 0 \\ \end{pmatrix}\begin{pmatrix}\psi_0\\ \psi_1 \\ e^{i\pi/4}\psi_0 \\ e^{i\pi/4}\psi_1 \end{pmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{pmatrix}\psi_0\\ \psi_1 \\ e^{i\pi/4}\psi_1 \\ e^{i\pi/4}\psi_0 \end{pmatrix} \end{align}

is not necessarily a separable state.

How do you go from here? The measurement is done on the top qubit only.

• Replacing the classically conditioned PX along the lines of en.wikipedia.org/wiki/Deferred_measurement_principle may be useful here. Commented Oct 6, 2023 at 5:20
• You are correct: the resulting state is only produced if the measurement of the top qubit is 1. Otherwise, the process must start again from scratch. (Incidentally, if the top qubit is measured to be 0 then the bottom qubit becomes $|\psi\rangle$, so the circuit can be rerun with that as input) Commented Oct 6, 2023 at 12:59

Consider the following circuit :

where $$|\psi\rangle$$ is a qubit in $$\mathbb{C}^2$$, $$|0\rangle= \begin{pmatrix}1 \\ 0 \end{pmatrix}$$, $$T= \begin{pmatrix}1 & 0\\ 0 & e^{i\pi/4} \end{pmatrix}$$ is the $$\pi/8$$ gate, $$H= \frac{1}{\sqrt{2}}\begin{pmatrix}1 & 1\\ 1 & -1 \end{pmatrix}$$ is the Hadamard gate, $$X= \begin{pmatrix}0 & 1\\ 1 & 0 \end{pmatrix}$$ and $$P= \begin{pmatrix}1 & 0\\ 0 & i \end{pmatrix}$$.

I'll get you part way there, and I'll work with the kets rather than the matrix representations (for the most part).

The input hits the first H and goes to: $$|0\rangle\otimes|\psi\rangle \to \frac{1}{\sqrt{2}}\left(|0\rangle+|1\rangle\right)\otimes|\psi\rangle$$ then hits the T gate and becomes: $$\to \frac{1}{\sqrt{2}}\left(|0\rangle+e^{i\phi}|1\rangle\right)\otimes|\psi\rangle\;,$$ which I'll write as $$e^{i\phi}$$, but this all works for $$\phi=\pi/4$$ too.

This hits the CNOT gate and becomes: $$\to \frac{1}{\sqrt{2}}\left\{|0\rangle\otimes|\psi\rangle+e^{i\phi}|1\rangle\otimes \left(\psi_1|0\rangle+\psi_0|1\rangle\right) \right\}$$ $$\frac{1}{\sqrt{2}}\left\{|0\rangle\otimes\left(\psi_0|0\rangle+\psi_1|1\rangle\right)+e^{i\phi}|1\rangle\otimes \left(\psi_1|0\rangle+\psi_0|1\rangle\right) \right\}$$ $$=\frac{1}{\sqrt{2}} \left\{ \left(\psi_0|0\rangle+e^{i\phi}\psi_1|1\rangle\right)\otimes|0\rangle + \left(\psi_1|0\rangle+e^{i\phi}\psi_0|1\rangle\right)\otimes|1\rangle \right\}\;.$$

If I measure 0 for the top ket (the rightmost in the direct product), then the states of the bottom key is (since no PX is applied): $$\left(\psi_0|0\rangle+e^{i\pi/4}\psi_1|1\rangle\right) = T|\psi\rangle$$

In order to show that the same is true when $$1$$ is measured for the top ket, use the fact that $$\phi$$ is really $$\pi/4$$ (the general case of $$\phi$$ won't work, it only works with $$T$$) and use the fact that $$PX$$ can be written as: $$PX = \left( \begin{matrix}1 & 0 \\ 0 & e^{i\pi/2} \end{matrix} \right) \left( \begin{matrix}0 & 1 \\ 1 & 0 \end{matrix} \right)\;,$$ where I have written $$i=e^{i\pi/2}$$.

And remember that you can always factor out an overall phase.