In previous post we learnt that:

measurements vs gates

but what about this:

mix measurement and gate

The books will tell you that we measure the first qubit and apply the Z gate to the second qubit. What does that even mean? Remember the fundamental object we are dealing with in a 2 qubit system is the \(4 \times 1\) wavefunction which is the joint probability distribution function of the 4 possible states the system can be in.

What do we do to the wavefunction to measure the first qubit and apply the Z gate to the second qubit? Can these two things even be done in parallel? It seems NO as the rule for measurement (Born’s rule) is different from the rule for doing time evolution (Schrodinger’s equation) or applying a gate in language of quantum computing.

So we can either perform the measurement followed by application of the gate or vice-versa. Will the two even commute?

\begin{equation} \textrm{Apply} (Z(q_2)) \cdot \textrm{Measure}(q_1) \stackrel{?}{=} \textrm{Measure}(q_1) \cdot \textrm{Apply} (Z(q_2)) \end{equation}

Let’s find out. Given a 2 qubit wavefunction:

\begin{equation} \left[\begin{matrix}a\\b\\c\\d\end{matrix}\right] \end{equation}

How do we measure the first qubit in \(\Psi\) - which is a \(4 \times 1\) vector - with a \(2 \times 2\) matrix \(Z\)? We need a \(4 \times 4\) matrix to measure a \(4 \times 1\) wavefunction. This matrix is given by \(I \otimes Z\). That is how we measure the “first qubit in \(\Psi\)”. We did this in previous post. When we do a measurement on the first qubit, it gives us two possible outcomes (the qubit will either collapse to 0 or 1). For each outcome, we will then apply the \(Z\) gate on the second qubit. The matrix for this will be given by \(Z \otimes I\).

This is what I find when I do this. The first outcome (this could be thought of as the qubit collapsing to 0) occurs with probability \(||a||^2 + ||b||^2\) and the final wavefunction after application of \(Z\) gate is:

\begin{equation} \left[\begin{matrix}\frac{a}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\- \frac{b}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\0\\0\end{matrix}\right] \end{equation}

The second outcome (qubit collapsing to 1) occurs with probability \(||c||^2 + ||d||^2\) and the final wavefunction after application of \(Z\) gate is:

\begin{equation} \left[\begin{matrix}0\\0\\\frac{c}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\\- \frac{d}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right] \end{equation}

Amazingly I get the same results if I instead apply the \(Z\) gate first to the second qubit and measure the first qubit after that! So the two do commute!

I then followed up this experiment and substituted the \(Z\) gate with \(Y\) and \(X\) gates. In all cases I found that the operations do commute. The result is the same whether we apply the gate first and measure after that or whether we measure first and apply the gate afterwards. This is quite remarkable because the measurement is an irreversible process and not something I had expected (but good relief). The results are summarized in the table below (do it as exercise, don’t take my word for it):

Table 1. Result of measuring the first qubit and applying a gate on second qubit. The order of the two operations does not matter.
Gate Outcome 1 (first qubit collapses to 0) Outcome 2 (first qubit collapses to 1)

\(\textbf{Z}\)

probability

\(\left|{a}\right|^{2} + \left|{b}\right|^{2}\)

\(\left|{c}\right|^{2} + \left|{d}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}\frac{a}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\- \frac{b}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\0\\0\end{matrix}\right)\)

\(\left(\begin{matrix}0\\0\\\frac{c}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\\- \frac{d}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right)\)

\(\textbf{Y}\)

probability

\(\left|{a}\right|^{2} + \left|{b}\right|^{2}\)

\(\left|{c}\right|^{2} + \left|{d}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}- \frac{i b}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\\frac{i a}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\0\\0\end{matrix}\right)\)

\(\left(\begin{matrix}0\\0\\- \frac{i d}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\\\frac{i c}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right)\)

\(\textbf{X}\)

probability

\(\left|{a}\right|^{2} + \left|{b}\right|^{2}\)

\(\left|{c}\right|^{2} + \left|{d}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}\frac{b}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\\frac{a}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\0\\0\end{matrix}\right)\)

\(\left(\begin{matrix}0\\0\\\frac{d}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\\\frac{c}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right)\)

My final experiment was to generalize using a gate \(G\) whose entries are given by:

\begin{equation} \begin{pmatrix} e && f \\ g && h \end{pmatrix} \end{equation}

In this case, the results are not that simple. Measuring the qubit first and applying the gate afterwards gives:

Table 2. First measure and then apply
Gate Outcome 1 Outcome 2

\(\textbf{G}\)

probability

\(\left|{a}\right|^{2} + \left|{b}\right|^{2}\)

\(\left|{c}\right|^{2} + \left|{d}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}\frac{a e}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}} + \frac{b f}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\\frac{a g}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}} + \frac{b h}{\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2}}}\\0\\0\end{matrix}\right)\)

\(\left(\begin{matrix}0\\0\\\frac{c e}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}} + \frac{d f}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\\\frac{c g}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}} + \frac{d h}{\sqrt{\left|{c}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right)\)

whereas applying the gate first and measuring the qubit afterwards gives:

Table 3. First apply and then measure
Gate Outcome 1 Outcome 2

\(\textbf{G}\)

probability

\(\left|{a e + b f}\right|^{2} + \left|{a g + b h}\right|^{2}\)

\(\left|{c e + d f}\right|^{2} + \left|{c g + d h}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}\frac{a e + b f}{\sqrt{\left|{a e + b f}\right|^{2} + \left|{a g + b h}\right|^{2}}}\\\frac{a g + b h}{\sqrt{\left|{a e + b f}\right|^{2} + \left|{a g + b h}\right|^{2}}}\\0\\0\end{matrix}\right)\)

\(\left(\begin{matrix}0\\0\\\frac{c e + d f}{\sqrt{\left|{c e + d f}\right|^{2} + \left|{c g + d h}\right|^{2}}}\\\frac{c g + d h}{\sqrt{\left|{c e + d f}\right|^{2} + \left|{c g + d h}\right|^{2}}}\end{matrix}\right)\)

The two cases seem to give different results. The tables don’t seem to be identical. However, remember that \(G\) has to be a unitary matrix meaning

\begin{equation} GG^\dagger = G^\dagger G = I \end{equation}

and this leads to some strong constraints on what \(e, f, g, h\) can be. Below are all the constraints that must apply to \(e, f, g, h\):

\begin{align} |e|^2 + |f|^2 & = 1 \\ |e|^2 + |g|^2 & = 1 \\ |g|^2 + |h|^2 & = 1 \\ |f|^2 + |h|^2 & = 1 \\ eg^* + fh^* & = 0 \\ e^* g + f^* h & = 0 \\ ef^* + gh^* & = 0 \\ e^* f + g^* h & = 0 \end{align}

With this and the fact that given two complex numbers \(z_1\) and \(z_2\):

\begin{equation} |z_1 + z_2|^2 = |z_1|^2 + |z_2|^2 + 2\textrm{Re}(z_1 z_2^*) \end{equation}

we have:

\begin{equation} |ae + bf|^2 = |a|^2 |e|^2 + |b|^2 |f|^2 + 2\textrm{Re}(ae b^* f^*) \end{equation}

and:

\begin{equation} |ag + bh|^2 = |a|^2 |g|^2 + |b|^2 |h|^2 + 2\textrm{Re}(ag b^* h^*) \end{equation}

and so:

\[\begin{equation} \begin{split} |ae + bf|^2 + |ag + bh|^2 & = |a|^2 (|e|^2 + |g|^2) + |b|^2 (|f|^2 + |h|^2) + 2\textrm{Re}(ab^* (ef^* + gh^*)) \\ & = |a|^2 \cdot 1 + |b|^2 \cdot 1 + 2\textrm{Re}(ab^* \cdot 0) \\ & = |a|^2 + |b|^2 \end{split} \end{equation}\]

Similarly one can show that:

\[\begin{equation} |ce + df|^2 + |cg + dh|^2 = |c|^2 + |d|^2 \end{equation}\]

and so the two tables are actually identical!. This is good news otherwise there would have been a big problem.

Khuda ka shukr hai, varna guzarti kaise shaam…​


One more thing before we wrap up. We have seen that to measure a qubit:

measure qubit

we need to form \(I \otimes Z\) and perform the measurement with this observable. The circuit can also be written as:

measure qubit equivalent circuit

where \(I\) is identity matrix. So putting 2 and 2 together if we instead have the circuit that we began with:

mix measurement and gate

What if we form \(Z \otimes Z\) and perform a measurement with \(M = Z \otimes Z\). What does that give? The answer is:

Table 4. Measure \(Z \otimes Z\)
Outcome 1 (both qubits are same 00 or 11) Outcome 2 (both qubits are different 01 or 10)

probability

\(\left|{a}\right|^{2} + \left|{d}\right|^{2}\)

\(\left|{b}\right|^{2} + \left|{c}\right|^{2}\)

\(\Psi\)

\(\left(\begin{matrix}\frac{a}{\sqrt{\left|{a}\right|^{2} + \left|{d}\right|^{2}}}\\0\\0\\\frac{d}{\sqrt{\left|{a}\right|^{2} + \left|{d}\right|^{2}}}\end{matrix}\right)\)

\(\left(\begin{matrix}0\\\frac{b}{\sqrt{\left|{b}\right|^{2} + \left|{c}\right|^{2}}}\\\frac{c}{\sqrt{\left|{b}\right|^{2} + \left|{c}\right|^{2}}}\\0\end{matrix}\right)\)

This is different from what we have seen earlier and is incorrect way of “measuring the first qubit and applying a gate on the second qubit”.

It is unfortunate that none of the books explain these things. But working them out on my own gives me the confidence that I understand at least some of this difficult subject.

I will never know myself until I do this on my own…​