Extras#

Objectives

  • to discover more single-qubit gates and their properties

  • to discover more entangled states.

Warning

You may need to refer to the programming exercises on the previous page whilst doing the activities on the current one. If so, open the previous page in a new tab, as reloading the current page will cause you to lose your progress.

 

Importing modules

Important

You must run this cell each time you reload the page!

import qiskit
import warnings
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import Sampler
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector, plot_histogram, visualize_transition

warnings.filterwarnings('ignore', category=DeprecationWarning)

qiskit.__version__   # The version of Qiskit we are using
'1.3.2'

More single-qubit gates#

The Y and Z-gates#

We’ve previously learnt about the X-gate which flips the state of a qubit from one to another. Let’s complete the set with the Y-gate and Z-gate.

 

🧠 CODING ACTIVITY 4 — Discover the action of the Y-gate

  1. Create a one-qubit circuit.

  2. Apply a Y-gate.

  3. Draw the statevector.

  4. Create another one-qubit circuit.

  5. Apply an X-gate.

  6. Apply a Y-gate

  7. Draw the statevector.

What state is each circuit in before applying the Y-gate?
What is the final state of each circuit?

# WRITE YOUR CODE HERE! 
# 
#
#
# MORE CODE SPACE
# 
# 
#

 

🧠 CODING ACTIVITY 5 — Discover the action of the Z-gate

  1. Create a one-qubit circuit.

  2. Apply a Z-gate.

  3. Draw the statevector.

  4. Create another one-qubit circuit.

  5. Apply an X-gate.

  6. Apply a Z-gate

  7. Draw the statevector.

What state is each circuit in before applying the Z-gate?
What is the final state of each circuit?

# WRITE YOUR CODE HERE! 
# 
#
#
# MORE CODE SPACE
# 
# 
#

 

Answers

The action of the Y-gate does the following transformations:

\[\ket{0}\xrightarrow{Y-gate}i\ket{1}\]
\[\ket{1}\xrightarrow{Y-gate}-i\ket{0}\]

The Y-gate changes flips the state of the qubit but also includes a complex phase of \(+i\) or \(-i\) accordingly.

The action of the Z-gate does the following transformations:

\[\ket{0}\xrightarrow{Z-gate}\ket{0}\]
\[\ket{1}\xrightarrow{Z-gate}-\ket{1}\]

The Z-gate does nothing to a qubit in state \(\ket{0}\) but places phase of \(-1\) onto the a qubit in state \(\ket{1}\).

y_circuit = QuantumCircuit(1) 
y_circuit.y(0)
y_circuit.draw("mpl")
y_state = Statevector(y_circuit)
y_state.draw("latex")

--- 

y_circuit = QuantumCircuit(1)
y_circuit.x(0)
y_circuit.y(0)
y_circuit.draw("mpl")
y_state = Statevector(y_circuit)
y_state.draw("latex")

---

z_circuit = QuantumCircuit(1) 
z_circuit.z(0)
z_circuit.draw("mpl")
z_state = Statevector(z_circuit)
z_state.draw("latex")


---

z_circuit = QuantumCircuit(1) 
z_circuit.x(0)
z_circuit.z(0)
z_circuit.draw("mpl")
z_state = Statevector(z_circuit)
z_state.draw("latex")

 

It is better to get a visual representation of the what is happening with each circuit. Let us now use the visualize_transition() function to get a better idea of what is happening.

 

🧠 CODING ACTIVITY 6 — Visualize the Y-gate operation

  1. Create a one-qubit circuit.

  2. Apply a Y-gate.

  3. Visualize the transition for the circuit.

In a new tab, revisit the section for the X-gate from the main programming exercise.
How is the transition for the Y-gate different to the X-gate transition?

# WRITE YOUR CODE HERE! 
# 
#
#

 

🧠 CODING ACTIVITY 7 — Visualize the Z-gate operation

  1. Create a one-qubit circuit.

  2. Apply a Z-gate.

  3. Visualize the transition for the circuit.

  4. Create another one-qubit circuit.

  5. Apply a Hadamard gate.

  6. Apply a Z-gate.

  7. Visualize the transition for the circuit.

How is the transition for the Z-gate different to the X and Y-gate transitions?
What do you notice about the angle of rotations for X, Y and Z-gates?

# WRITE YOUR CODE HERE! 
# 
#
#
# MORE CODE SPACE
# 
#
#

 

Answers

The axis of rotation for the Y-gate is different to the X-gate. The Z-gate appears to do nothing in the first circuit, but in the second circuit there is rotation. The angles of rotations are all \(180^{\circ}\). To be more specific, the X, Y and Z-gates are rotations of \(180^{\circ}\) about the x-axis, the y-axis and the z-axis respectively.

y_circuit = QuantumCircuit(1) 
y_circuit.y(0)
visualize_transition(y_circuit, trace=True, fpg=30, spg=2)

---

z_circuit = QuantumCircuit(1) 
z_circuit.z(0)
visualize_transition(z_circuit, trace=True, fpg=30, spg=2)

---

z_circuit = QuantumCircuit(1) 
z_circuit.h(0)
z_circuit.z(0)
visualize_transition(z_circuit, trace=True, fpg=30, spg=2)

 

Note

To summarize, we have discovered the actions of the X,Y and Z-gates in terms of rotation of the qubit state about these axes. We’ve also seen that some gates transform qubits by changing the state and/or changing their phases. Both are important for describing the overall state of the qubit.

 

More on entangled states#

Two-qubit entanglement#

In CODING ACTIVITY 3 of the main programming exercises, we were able to create an entangled state where the qubit outcomes are always the same (“00” or “11”) using one Hadamard gate and one CX-gate.

 

🧠 CODING ACTIVITY 8 — Entangled opposites

Create a two-qubit entangled state where the outcomes must always be opposite. Verify your state by either drawing the statevector or by executing the circuit and plotting the histogram.

Hint

Only 3 gates are needed to create this state.

# WRITE CODE HERE! 
#
#
#
# MORE CODE SPACE
#
#
#
# EXTRA IF NEEDED
#
#
#

 

Answer

In this state, the outcomes “00” and “11” are forbidden.

We only need to add one more gate from the circuit in CODING ACTIVITY 3. It’s an X-gate which flips the state of one of the qubits. There are a few possibilities. Either, you place the X-gate on the second qubit before or after the CX-gate. Or, you can place the X-gate on the first qubit after the CX-gate.

The final state is \( \frac{\ket{01} + \ket{10}}{\sqrt{2}}\).

Here are the three circuit options.

opp_entangled = QuantumCircuit(2)
opp_entangled.h(0)
opp_entangled.x(1)
opp_entangled.cx(0,1)
opp_entangled.draw("mpl")

---

opp_entangled = QuantumCircuit(2)
opp_entangled.h(0)
opp_entangled.cx(0,1)
opp_entangled.x(1)
opp_entangled.draw("mpl")
---

opp_entangled = QuantumCircuit(2)
opp_entangled.h(0)
opp_entangled.cx(0,1)
opp_entangled.x(0)
opp_entangled.draw("mpl")

 

Three-qubit entanglement#

Until now, the discussion has only been about entangling two qubits. But, three or more qubits may be entangled.

 

🧠 CODING ACTIVITY 9 — GHZ state

Create a three-qubit entangled state where the outcomes of each are always the same. The final state of the circuit should be

\[ \frac{\ket{000} + \ket{111}}{\sqrt{2}} \]

This is called the Greenberger–Horne–Zeilinger or GHZ state.

# WRITE CODE HERE! 
# 
#
#
# MORE CODE SPACE
# 
# 
#

 

Answer

We only need to add one more qubit and one more gate from the circuit in CODING ACTIVITY 3. The extra gate is another CX-gate which targets the third qubit but may be controlled by either the first or second qubit. This ensures that the state of the third qubit is flipped once of the first two qubits have flipped.

Here are the options.

ghz_state = QuantumCircuit(3)
ghz_state.h(0)
ghz_state.cx(0,1)
ghz_state.cx(0,2)
ghz_state.draw("mpl")

---

ghz_state = QuantumCircuit(3)
ghz_state.h(0)
ghz_state.cx(0,1)
ghz_state.cx(1,2)
ghz_state.draw("mpl")

Â