The following code originates from a third-party, although, I have added the commented line containing "UNCOMMENT THE START OF THIS LINE".

import random
import numpy as np
import cirq

# Create a circuit
circuit = cirq.Circuit()
(q0, q1) = cirq.LineQubit.range(2)

# Apply the X-Pauli gate to each qubit
circuit.append([cirq.X(q0), cirq.X(q1)])

# Apply the Hadamard gate to first qubit and CNOT gate to both qubits
circuit.append([cirq.H(q0), cirq.CNOT(q0, q1)])

# Make a change to q1, to see if q0 shows a corresponding change
#circuit.append([cirq.X(q1)]) # UNCOMMENT THE START OF THIS LINE

#Measure both qubits
circuit.append([cirq.measure(q0), cirq.measure(q1)])

#Print the Circuit

# Simulate the circuit several times.
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=10)

#Print the results

The code is intended to demonstrate entanglement, which the output suggests it achieves.

0: ───X───H───@───M───
1: ───X───────X───M───


Yet, if the commented line I added is uncommented, and the code run, the output no longer suggests entanglement.

0: ───X───H───@───M───────
1: ───X───────X───X───M───


I expected the q(0) results to be the opposite of those seen for q(1).

That is what I understand entanglement would achieve; you change q(1) and q(0) changes too.

As such, at the moment, the original code is not producing entanglement at all, but, merely, forcing the result to show opposite values; giving the illusion of entanglement.

Is there something I'm missing that will further my understanding, or, perhaps, even some Python/cirq code that delivers a simulation of entanglement that sees qubits behave as I'm expecting them to?


1 Answer 1


The problem is that you negate qubit q1 before the measurement. As both qubits are opposite each other under original settings (see the first circuit results), negation gate you added on q1 leads effectively to state where both qubits have same value. See more on construction of Bell states here.

You can see this also by direct calculation. As your input is $|11\rangle$, Hadamard gate together with CNOT prepare state $|\psi\rangle =\frac{1}{\sqrt{2}}(|01\rangle - |10\rangle) = \frac{1}{\sqrt{2}}(0,1,-1,0)^{T}$ (seee link above why this is true). Then you apply gate $X$ on qubit q1. This means that gate $I \otimes X$ is applied on both qubits ($I$ is identical operator), so

$$ (I \otimes X)|\psi\rangle = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \otimes \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} |\psi\rangle = \frac{1}{\sqrt{2}} \begin{pmatrix} 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} \begin{pmatrix} 0 \\ 1 \\ -1 \\ 0 \end{pmatrix} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 \\ 0 \\ 0 \\ -1 \end{pmatrix} = \frac{1}{\sqrt{2}}(|00\rangle - |11\rangle). $$

The phase $-1$ disappear after measurement in computational basis and your are left with state having equal probabilities of outcomes $00$ and $11$.

As you can see, you transformed one Bell state to another one, so entanglemnt is preserved (note that Bell states are maximally entangled ones).

Overall, your code behaves according to expectations.

  • 1
    $\begingroup$ Thanks for your response. I do not doubt the mathematics. I was expecting the cirq simulator to provide entanglement, within a circuit, which is then maintained beyond the moment that entanglement occurs. The code examples I have found do not fulfil that expectation. Instead, they illustrate measurements entanglement would deliver, when initially measured, but do not allow for further measurement following a change being made to either qubit. Negating q1 should see q0 affected, whereas, only q1 is changed. This, to my understanding, means q0 and q1 were not entangled. $\endgroup$
    – beaver
    Feb 28 at 11:32
  • $\begingroup$ @beaver: Let me shed more light on this. As you can see from my calculation, an input to gate $I \otimes X$ is one entangled state $|01\rangle - |10\rangle$ and output is another entangled state $|00\rangle - |11\rangle$. This means that the entanglement is preserved. You can convince yourself that the states are entangled by prooving that they cannot be expressed as Kronecker (tensor) product $\otimes$ of two single-qubit states. $\endgroup$ Feb 28 at 12:05
  • $\begingroup$ @beaver: What is more, once you measure either qubit in case of state $|01\rangle - |10\rangle$, you automatically know the other one. If first one is 0, then second one is 1 and vice versa. This is also true for state $|00\rangle - |11\rangle$ - if first qubit is zero (one), second one is also zero (one). If you know whole multi-qubit state by measuring just one qubit of the state, it implies that the multi-qubit state is entangled (or to be precise, maximally entangled). $\endgroup$ Feb 28 at 12:08
  • $\begingroup$ From my experience, the cirq simulator is not delivering the math. $\endgroup$
    – beaver
    Feb 29 at 11:16
  • $\begingroup$ @beaver: Sorry, I do not understand. The simulator is of course based on mathematical model of quantum computing. Yes, there can be rounding errors etc. but math certainly directs actions of the simulator. $\endgroup$ Feb 29 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.