{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "remove_cell"
    ]
   },
   "source": [
    "# Exercise Sheet 1: Quantum Logic Gates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To run this notebook you'll need to [install `qiskit`](https://docs.quantum.ibm.com/guides/install-qiskit), `qiskit-aer` and `qiskit-ibm-runtime`. This can be done in the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install qiskit\n",
    "!pip install qiskit-aer\n",
    "!pip install qiskit-ibm-runtime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit, transpile\n",
    "from qiskit_aer import AerSimulator\n",
    "from qiskit.visualization import plot_histogram\n",
    "import numpy as np\n",
    "\n",
    "from tests1 import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview\n",
    "## **Exercise 1**\n",
    "\n",
    "See 'Part 1' below, and find and complete the circuits required for the:\n",
    "* (a) `XOR` gate;\n",
    "* (b) `AND` gate;\n",
    "* (c) `NAND` gate;\n",
    "* (d) `OR` gate.\n",
    "\n",
    "'Part 2' below is for your own interest, where you will find a `layout` for which the AND gate compiles to 6 non-local gates for `Manila`. Note that there is some randomness in the compiling process. So you might need to try a few times. Feel free to transipile the other gates you have designed. \n",
    "\n",
    "## **Excercise 2** \n",
    "Answers Code showing various properties of foundational operations such as the Pauli gates/matrices and the Hadamard gate/matrix and some practice on alternative bases. \n",
    "* 1) (a)(b) Alternative Pauli Basis States\n",
    "* 2) (a)(b)(c)(d) Properties of Pauli Matrices\n",
    "* 3) (a)(b)(c) The Hadamard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 1\n",
    "<h2 style=\"font-size:24px;\">Part 1: Classical logic gates with quantum circuits</h2>\n",
    "\n",
    "<br>\n",
    "\n",
    "<h3 style=\"font-size: 20px\">&#128211; NOT gate (an example)</h3>\n",
    "\n",
    "In the following function we use a quantum circuit to do the simplest job in all of computation: apply a NOT gate to a bit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def NOT(inp):\n",
    "    \"\"\"An NOT gate.\n",
    "    \n",
    "    Parameters:\n",
    "        inp (str): Input, encoded in qubit 0.\n",
    "        \n",
    "    Returns:\n",
    "        QuantumCircuit: Output NOT circuit.\n",
    "        str: Output value measured from qubit 0.\n",
    "    \"\"\"\n",
    "\n",
    "    qc = QuantumCircuit(1, 1) # A quantum circuit with a single qubit and a single classical bit\n",
    "    qc.reset(0)\n",
    "    \n",
    "    # We encode '0' as the qubit state |0⟩, and '1' as |1⟩\n",
    "    # Since the qubit is initially |0⟩, we don't need to do anything for an input of '0'\n",
    "    # For an input of '1', we do an x to rotate the |0⟩ to |1⟩\n",
    "    if inp=='1':\n",
    "        qc.x(0)\n",
    "        \n",
    "    # barrier between input state and gate operation \n",
    "    qc.barrier()\n",
    "    \n",
    "    # Now we've encoded the input, we can do a NOT on it using x\n",
    "    qc.x(0)\n",
    "    \n",
    "    #barrier between gate operation and measurement\n",
    "    qc.barrier()\n",
    "    \n",
    "    # Finally, we extract the |0⟩/|1⟩ output of the qubit and encode it in the bit c[0]\n",
    "    qc.measure(0,0)\n",
    "    \n",
    "    # We'll run the program on a simulator\n",
    "    backend = AerSimulator()\n",
    "    # Since the output will be deterministic, we can use just a single shot to get it\n",
    "    job = backend.run(qc, shots=1, memory=True)\n",
    "    output = job.result().get_memory()[0]\n",
    "    \n",
    "    return qc, output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's see it in action."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for inp in ['0', '1']:\n",
    "    qc, out = NOT(inp)\n",
    "    print('NOT with input',inp,'gives output',out)\n",
    "    display(qc.draw())\n",
    "    print('\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This implementation of NOT gate was an example of what you will need to do for a range of other gates below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"font-size: 20px\">&#128211;(a) XOR gate </h3>\n",
    "\n",
    "Takes two binary strings as input and gives one as output.\n",
    "\n",
    "The output is '0' when the inputs are equal and  '1' otherwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "def XOR(inp1,inp2):\n",
    "    \"\"\"An XOR gate.\n",
    "    \n",
    "    Parameters:\n",
    "        inpt1 (str): Input 1, encoded in qubit 0.\n",
    "        inpt2 (str): Input 2, encoded in qubit 1.\n",
    "        \n",
    "    Returns:\n",
    "        QuantumCircuit: Output XOR circuit.\n",
    "        str: Output value measured from qubit 1.\n",
    "    \"\"\"\n",
    "  \n",
    "    qc = QuantumCircuit(2, 1) \n",
    "    qc.reset(range(2))\n",
    "    \n",
    "    if inp1=='1':\n",
    "        qc.x(0)\n",
    "    if inp2=='1':\n",
    "        qc.x(1)\n",
    "    \n",
    "    # barrier between input state and gate operation \n",
    "    qc.barrier()\n",
    "    \n",
    "    # this is where your program for quantum XOR gate goes\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    # barrier between input state and gate operation \n",
    "    qc.barrier()\n",
    "    \n",
    "    qc.measure(1,0) # output from qubit 1 is measured\n",
    "  \n",
    "    #We'll run the program on a simulator\n",
    "    backend = AerSimulator()\n",
    "    #Since the output will be deterministic, we can use just a single shot to get it\n",
    "    job = backend.run(qc, shots=1, memory=True)\n",
    "    output = job.result().get_memory()[0]\n",
    "  \n",
    "    return qc, output\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Test the function\n",
    "for inp1 in ['0', '1']:\n",
    "    for inp2 in ['0', '1']:\n",
    "        qc, output = XOR(inp1, inp2)\n",
    "        print('XOR with inputs',inp1,inp2,'gives output',output)\n",
    "        display(qc.draw())\n",
    "        print('\\n')\n",
    "\n",
    "test_xor(XOR)  # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"font-size: 20px\">&#128211; (b) AND gate</h3>\n",
    "\n",
    "Takes two binary strings as input and gives one as output.\n",
    "\n",
    "The output is `'1'` only when both the inputs are `'1'`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "\n",
    "def AND(inp1,inp2):\n",
    "    \"\"\"An AND gate.\n",
    "    \n",
    "    Parameters:\n",
    "        inpt1 (str): Input 1, encoded in qubit 0.\n",
    "        inpt2 (str): Input 2, encoded in qubit 1.\n",
    "        \n",
    "    Returns:\n",
    "        QuantumCircuit: Output XOR circuit.\n",
    "        str: Output value measured from qubit 2.\n",
    "    \"\"\"\n",
    "    qc = QuantumCircuit(3, 1) \n",
    "    qc.reset(range(2))\n",
    "  \n",
    "    if inp1=='1':\n",
    "        qc.x(0)\n",
    "    if inp2=='1':\n",
    "        qc.x(1)\n",
    "        \n",
    "    qc.barrier()\n",
    "\n",
    "    # this is where your program for quantum AND gate goes\n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "\n",
    "    qc.barrier()\n",
    "    qc.measure(2, 0) # output from qubit 2 is measured\n",
    "  \n",
    "    # We'll run the program on a simulator\n",
    "    backend = AerSimulator()\n",
    "    #Since the output will be deterministic, we can use just a single shot to get it\n",
    "    job = backend.run(qc, shots=1, memory=True)\n",
    "    output = job.result().get_memory()[0]\n",
    "  \n",
    "    return qc, output\n",
    "    # Test AND gate outputs using assert statements\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Test the function\n",
    "for inp1 in ['0', '1']:\n",
    "    for inp2 in ['0', '1']:\n",
    "        qc, output = AND(inp1, inp2)\n",
    "        print('AND with inputs',inp1,inp2,'gives output',output)\n",
    "        display(qc.draw())\n",
    "        print('\\n')\n",
    "\n",
    "test_and(AND) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"font-size: 20px\">&#128211; NAND gate</h3>\n",
    "\n",
    "Takes two binary strings as input and gives one as output.\n",
    "\n",
    "The output is `'0'` only when both the inputs are `'1'`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "def NAND(inp1,inp2):\n",
    "    \"\"\"An NAND gate.\n",
    "    \n",
    "    Parameters:\n",
    "        inpt1 (str): Input 1, encoded in qubit 0.\n",
    "        inpt2 (str): Input 2, encoded in qubit 1.\n",
    "        \n",
    "    Returns:\n",
    "        QuantumCircuit: Output NAND circuit.\n",
    "        str: Output value measured from qubit 2.\n",
    "    \"\"\"\n",
    "    qc = QuantumCircuit(3, 1) \n",
    "    qc.reset(range(3))\n",
    "    \n",
    "    if inp1=='1':\n",
    "        qc.x(0)\n",
    "    if inp2=='1':\n",
    "        qc.x(1)\n",
    "    \n",
    "    qc.barrier()\n",
    "    \n",
    "    # this is where your program for quantum NAND gate goes\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    qc.barrier()\n",
    "    qc.measure(2, 0) # output from qubit 2 is measured\n",
    "  \n",
    "    # We'll run the program on a simulator\n",
    "    backend = AerSimulator()\n",
    "    #Since the output will be deterministic, we can use just a single shot to get it\n",
    "    job = backend.run(qc, shots=1, memory=True)\n",
    "    output = job.result().get_memory()[0]\n",
    "  \n",
    "    return qc, output\n",
    "    # Test NAND gate outputs using assert statements\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Test the function\n",
    "for inp1 in ['0', '1']:\n",
    "    for inp2 in ['0', '1']:\n",
    "        qc, output = NAND(inp1, inp2)\n",
    "        print('NAND with inputs',inp1,inp2,'gives output',output)\n",
    "        display(qc.draw())\n",
    "        print('\\n')\n",
    "\n",
    "test_nand(NAND) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3 style=\"font-size: 20px\">&#128211; OR gate</h3>\n",
    "\n",
    "Takes two binary strings as input and gives one as output.\n",
    "\n",
    "The output is '1' if either input is '1'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "def OR(inp1,inp2):\n",
    "    \"\"\"An OR gate.\n",
    "    \n",
    "    Parameters:\n",
    "        inpt1 (str): Input 1, encoded in qubit 0.\n",
    "        inpt2 (str): Input 2, encoded in qubit 1.\n",
    "        \n",
    "    Returns:\n",
    "        QuantumCircuit: Output XOR circuit.\n",
    "        str: Output value measured from qubit 2.\n",
    "    \"\"\"\n",
    "\n",
    "    qc = QuantumCircuit(3, 1) \n",
    "    qc.reset(range(3))\n",
    "    \n",
    "    if inp1=='1':\n",
    "        qc.x(0)\n",
    "    if inp2=='1':\n",
    "        qc.x(1)\n",
    "    \n",
    "    qc.barrier()\n",
    "   \n",
    "    # this is where your program for quantum OR gate goes\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    qc.barrier()\n",
    "    qc.measure(2, 0) # output from qubit 2 is measured\n",
    "  \n",
    "    # We'll run the program on a simulator\n",
    "    backend = AerSimulator()\n",
    "    #Since the output will be deterministic, we can use just a single shot to get it\n",
    "    job = backend.run(qc, shots=1, memory=True)\n",
    "    output = job.result().get_memory()[0]\n",
    "  \n",
    "    return qc, output\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Test the function\n",
    "for inp1 in ['0', '1']:\n",
    "    for inp2 in ['0', '1']:\n",
    "        qc, output = OR(inp1, inp2)\n",
    "        print('OR with inputs',inp1,inp2,'gives output',output)\n",
    "        display(qc.draw())\n",
    "        print('\\n')\n",
    "\n",
    "test_or(OR) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2 style=\"font-size:24px;\">Part 2: AND gate on Quantum Computer</h2>\n",
    "<br>\n",
    "\n",
    "Real quantum computers are not able to implement arbitary gates directly. Instead, everything needs to be compiled  (or 'transpiled') to the set of basic gates that the device can use. This usually consists of a set of single qubit rotations, as well as two qubit gates like `cx`.\n",
    "\n",
    "There are also limits on which `cx` gates can be used directly: only some pairs of control and target qubits are possible. To implement other `cx` gates, tricks such as using `swap` gates to effectively move information around must be used. The possible pairs of qubits on which `cx` gates can be applied is known as the 'connectivity' of the device.\n",
    "\n",
    "We'll now look at some examples. To make sure you don't end up in a queue for a busy device, we'll be using mock backends. These are designed to act exactly like real backends."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_ibm_runtime.fake_provider import FakeYorktownV2\n",
    "backend = FakeYorktownV2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The system we are using (or at least pretending to) is `Manila`. With this system, it is not possible to peform a `cx` gate between any arbitrary pair of qubits. Instead only the following pairings are allowed (where the qubits are labelled from 0 to 4)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "backend.configuration().coupling_map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The following cell has a circuit that applies an `AND` gate that is constructed from single and two qubit gates. However, this circuit assumes full connectivity: that `cx` gates can be applied between any pair of qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qc_and = QuantumCircuit(3)\n",
    "qc_and.ccx(0,1,2)\n",
    "print('AND gate')\n",
    "display(qc_and.draw())\n",
    "print('\\n\\nTranspiled AND gate for hardware with the required connectiviy')\n",
    "qc_and.decompose().draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This ideal transpilation requires 6 `cx` gates.\n",
    "\n",
    "There are often optimizations that the transpiler can perform that reduce the overall gate count, and thus total length of the input circuits.  Note that the addition of swaps to match the device topology, and optimizations for reducing the length of a circuit are at odds with each other. In what follows we will make use of `initial_layout` that allows us to pick the qubits on a device used for the computation and `optimization_level`, an argument that allows selecting from internal defaults for circuit swap mapping and optimization methods to perform."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rather than actually running the AND function, let's just look at the transpiled circuits. The following function does this for a given set of inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the cell to define AND gate for real quantum system\n",
    "\n",
    "def AND(inp1, inp2, backend, layout):\n",
    "    \n",
    "    qc = QuantumCircuit(3, 1) \n",
    "    qc.reset(range(3))\n",
    "    \n",
    "    if inp1=='1':\n",
    "        qc.x(0)\n",
    "    if inp2=='1':\n",
    "        qc.x(1)\n",
    "        \n",
    "    qc.barrier()\n",
    "    qc.ccx(0, 1, 2) \n",
    "    qc.barrier()\n",
    "    qc.measure(2, 0) \n",
    "  \n",
    "    qc_trans = transpile(qc, backend, initial_layout=layout, optimization_level=3)\n",
    "    \n",
    "    return qc_trans"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p>&#128211; Assign your choice of layout to the list variable <code>layout</code> in the cell below</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Assign your choice of the initial_layout to the variable layout1 as a list \n",
    "# For example\n",
    "#     layout = [0,2,4]\n",
    "layout = [0,2,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compile the `AND` gate on `Manila` by running the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for input1 in ['0','1']:\n",
    "    for input2 in ['0','1']:\n",
    "        qc_trans1 = AND(input1, input2, backend, layout)\n",
    "                \n",
    "        print('For input '+input1+input2)\n",
    "        print('# of nonlocal gates =',qc_trans1.num_nonlocal_gates())\n",
    "\n",
    "test_compilation(layout) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Excercise 2\n",
    "## 1. Alternative Pauli Basis States\n",
    "\n",
    "There are an infinite number of possible single qubit states.  From a theoretical stand-point,  the  one  we  choose  to  label $| 0 \\rangle$ is  arbitrary.   So  let’s  consider  the  following alternative.\n",
    "\n",
    "$$\n",
    "| \\bar 0 \\rangle = \\cos(\\theta) \\, | 0 \\rangle + \\sin(\\theta) \\, | 1 \\rangle.\n",
    "$$\n",
    "\n",
    "For this $| \\bar 0 \\rangle$:\n",
    "\n",
    "(a) Find a corresponding orthogonal state $| \\bar 1 \\rangle$;\n",
    "\n",
    "(b)  For this basis $| \\bar 0 \\rangle$, $| \\bar 1 \\rangle$, find mutually unbiased basis states $| \\bar + \\rangle$ and $| \\bar - \\rangle$;\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Instructions\n",
    "\n",
    "**Representing States**: Qubit states will be represented as NumPy arrays of shape (2, 1). For example, ket_0 = np.array([[1], [0]]).\n",
    "\n",
    "**Completing the Functions**: Your task is to complete the three Python functions provided in the code block below:\n",
    "\n",
    "`find_orthogonal_state(ket_zero_bar, theta)`\n",
    "\n",
    "`find_mutually_unbiased_basis_plus(ket_zero_bar, ket_one_bar)`\n",
    "\n",
    "`find_mutually_unbiased_basis_minus(ket_zero_bar, ket_one_bar)`\n",
    "\n",
    "**Running the Tests**: After implementing the functions, run the entire script. The assert statements will automatically check if your implementations are correct. If all tests pass, you will see the message \"All tests passed!\". Otherwise, an AssertionError will be raised, indicating a problem with your implementation.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ket_0 = np.array([[1], [0]], dtype=complex)\n",
    "ket_1 = np.array([[0], [1]], dtype=complex)\n",
    "\n",
    "### BEGIN SOLUTION\n",
    "def find_orthogonal_state(ket_zero_bar, theta):\n",
    "    \"\"\"\n",
    "    Calculates the orthogonal state |ψ1⟩ for a given |ψ0⟩.\n",
    "\n",
    "    Args:\n",
    "        ket_zero_bar: A NumPy array representing the state |ψ0⟩.\n",
    "        theta: The angle theta in radians.\n",
    "\n",
    "    Returns:\n",
    "        A NumPy array representing the orthogonal state |ψ1⟩.\n",
    "    \"\"\"\n",
    "    # --- YOUR CODE HERE ---\n",
    "    # Hint: An orthogonal state can be found by swapping the amplitudes\n",
    "    # of the original state and changing the sign of one of them.\n",
    "    # For |ψ0⟩ = a|0⟩ + b|1⟩, an orthogonal state is |ψ1⟩ = b|0⟩ - a|1⟩.\n",
    "    # Another valid orthogonal state is |ψ1⟩ = -b|0⟩ + a|1⟩.\n",
    "\n",
    "    pass # Replace this with your implementation\n",
    "\n",
    "def find_mutually_unbiased_basis_plus(ket_zero_bar, ket_one_bar):\n",
    "    \"\"\"\n",
    "    Calculates the |ψ+⟩ state for the mutually unbiased basis.\n",
    "\n",
    "    Args:\n",
    "        ket_zero_bar: A NumPy array representing the state |ψ0⟩.\n",
    "        ket_one_bar: A NumPy array representing the state |ψ1⟩.\n",
    "\n",
    "    Returns:\n",
    "        A NumPy array representing the |ψ+⟩ state.\n",
    "    \"\"\"\n",
    "    # --- YOUR CODE HERE ---\n",
    "    # Hint: The |+⟩ state in the standard basis is (|0⟩ + |1⟩) / sqrt(2).\n",
    "    # In a new basis, it will be a superposition of the new basis vectors.\n",
    "\n",
    "    pass # Replace this with your implementation\n",
    "\n",
    "def find_mutually_unbiased_basis_minus(ket_zero_bar, ket_one_bar):\n",
    "    \"\"\"\n",
    "    Calculates the |ψ-⟩ state for the mutually unbiased basis.\n",
    "\n",
    "    Args:\n",
    "        ket_zero_bar: A NumPy array representing the state |ψ0⟩.\n",
    "        ket_one_bar: A NumPy array representing the state |ψ1⟩.\n",
    "\n",
    "    Returns:\n",
    "        A NumPy array representing the |ψ-⟩ state.\n",
    "    \"\"\"\n",
    "    # --- YOUR CODE HERE ---\n",
    "    # Hint: The |-⟩ state in the standard basis is (|0⟩ - |1⟩) / sqrt(2).\n",
    "    # In a new basis, it will also be a superposition of the new basis vectors.\n",
    "\n",
    "    pass # Replace this with your implementation\n",
    "### END SOLTUION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test your solutions\n",
    "test_alt(find_orthogonal_state,find_mutually_unbiased_basis_plus,find_mutually_unbiased_basis_minus) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Properties of the Pauli Matrices\n",
    "\n",
    "Note: Sometimes the Pauli matrices are written as $X$, $Y$ and $Z$, and sometimes as $\\sigma_x$, $\\sigma_y$ and $\\sigma_z$. For  the  most  part,  the  convention  is  an  arbitrary  choice. Once  you’ve used them enough,  you’ll hardly notice the difference (to the great annoyance of your students!).\n",
    "\n",
    "The Pauli matrices are defined\n",
    "\n",
    "$$\n",
    "X = \n",
    "\\begin{pmatrix} \n",
    "0 & 1 \\\\\n",
    "1 & 0 \\\\\n",
    "\\end{pmatrix}, \\,\\,\n",
    "Y = \n",
    "\\begin{pmatrix} \n",
    "0 & -i \\\\\n",
    "i & 0 \\\\\n",
    "\\end{pmatrix}, \\,\\,\n",
    "Z = \n",
    "\\begin{pmatrix} \n",
    "1 & 0 \\\\\n",
    "0 & -1 \\\\\n",
    "\\end{pmatrix}, \\,\\,\n",
    "$$\n",
    "\n",
    "(a) Show that each squares to the identity matrix.\n",
    "\n",
    "$$\n",
    "I = \n",
    "\\begin{pmatrix} \n",
    "1 & 0 \\\\\n",
    "0 & 1 \\\\\n",
    "\\end{pmatrix}\n",
    "$$\n",
    "\n",
    "(b) Show that $P_1 P_2 = - P_2 P_1$ for any pair of Paulis $P_1$ and $P_2$.\n",
    "\n",
    "(c) Show that $P_1 P_2 \\sim P_3$ for any pair of Paulis $P_1$ and $P_2$, where $P_3$ is the remaining Pauli.\n",
    "\n",
    "(d) Find the eigenvectors and eigenvalues of each Pauli.\n",
    "\n",
    "### Instructions \n",
    "\n",
    "**Define the Matrices**: In Part 1, define the Pauli matrices X, Y, Z, and the identity matrix I using NumPy.\n",
    "\n",
    "**Implement the Test Functions**: In Part 2, complete the function skeletons provided. Each function is designed to test a specific property of the Pauli matrices and should return a value (usually a boolean) indicating the result of the test. Read the docstrings and hints carefully to understand what each function needs to do.\n",
    "\n",
    "**Sumbit**: After completing your implementation, run the entire script. The code in the test section will call your functions and use assert statements to check if they produce the correct results. If all your functions work as expected, you will see a series of \"PASS\" messages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "# --- Part 1: Define the Matrices ---\n",
    "# Define the Pauli matrices and the Identity matrix as NumPy arrays.\n",
    "# Use dtype=complex for matrices with complex entries.\n",
    "\n",
    "X = None  # Replace None with the definition of the X matrix\n",
    "Y = None  # Replace None with the definition of the Y matrix\n",
    "Z = None  # Replace None with the definition of the Z matrix\n",
    "I = None  # Replace None with the definition of the Identity matrix\n",
    "\n",
    "# This dictionary is used by the test functions.\n",
    "paulis = {'X': X, 'Y': Y, 'Z': Z}\n",
    "\n",
    "# --- Part 2: Implement the Test Functions ---\n",
    "# Complete the following functions to test the properties of the Pauli matrices.\n",
    "\n",
    "def test_square_to_identity(P):\n",
    "    \"\"\"\n",
    "    (a) Check if each Pauli matrix squares to the identity matrix.\n",
    "\n",
    "    Returns:\n",
    "        np.array: matrix of your results \n",
    "    \"\"\"\n",
    "    # Hint: \n",
    "    # calculate P @ P\n",
    "    pass  # Your implementation here\n",
    "\n",
    "def test_anticommutation(P1, P2):\n",
    "    \"\"\"\n",
    "    (b) Check if P1 @ P2 = -P2 @ P1 for any pair of distinct Paulis.\n",
    "\n",
    "    Returns:\n",
    "        [np.array1,np.array2]: list of the left and right side of the equation for one pair\n",
    "    \"\"\"\n",
    "    # Hint: You can check the pairs (X, Y), (Y, Z), and (Z, X).\n",
    "    # For each pair (P1, P2), check if P1 @ P2 is close to -(P2 @ P1).\n",
    "    pass  # Your implementation here\n",
    "\n",
    "def test_product_relation():\n",
    "    \"\"\"\n",
    "    (c) Check if the Pauli product relations hold (XY=iZ, YZ=iX, ZX=iY).\n",
    "\n",
    "    Returns:\n",
    "        list[np.array]: The 3 matrices resulting from your calculations in the order above\n",
    "    \"\"\"\n",
    "    # Hint: Check each of the three relations separately. For example,\n",
    "    # check if np.allclose(X @ Y, 1j * Z).\n",
    "    # All three must be correct for the function to pass.\n",
    "    pass  # Your implementation here\n",
    "\n",
    "def get_eigenvalues_and_eigenvectors():\n",
    "    \"\"\"\n",
    "    (d) Calculate the eigenvalues and eigenvectors for each Pauli matrix.\n",
    "\n",
    "    Returns:\n",
    "        tuple: A tuple containing two dictionaries:\n",
    "               (eigenvalues_dict, eigenvectors_dict)\n",
    "\n",
    "               - eigenvalues_dict: A dictionary where keys are the names\n",
    "                 ('X', 'Y', 'Z') and values are the corresponding eigenvalues.\n",
    "               - eigenvectors_dict: A dictionary where keys are the names\n",
    "                 and values are the corresponding eigenvectors.\n",
    "    \"\"\"\n",
    "    # Hint: Use a loop and np.linalg.eig() for each Pauli matrix. This\n",
    "    # function returns a tuple of (eigenvalues, eigenvectors). Store these\n",
    "    # in the two dictionaries with the correct keys ('X', 'Y', 'Z').\n",
    "    eigenvalues_dict = {}\n",
    "    eigenvectors_dict = {}\n",
    "\n",
    "    # Your implementation here\n",
    "\n",
    "    return eigenvalues_dict, eigenvectors_dict\n",
    "\n",
    "### END SOLUTION\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test your solutions\n",
    "test_paulis(I,X,Y,Z,test_square_to_identity,test_anticommutation,test_product_relation,get_eigenvalues_and_eigenvectors) # DO NOT EDIT THIS LINE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 3. The Hadamard\n",
    "\n",
    "The Hadamard matrix can be expressed\n",
    "\n",
    "$$\n",
    "H = \\frac{1}{\\sqrt{2}}\n",
    "\\begin{pmatrix} \n",
    "1 & 1 \\\\\n",
    "1 & -1 \\\\\n",
    "\\end{pmatrix}\n",
    "$$\n",
    "\n",
    "(a) Find the eigenvectors and eigenvalues of this matrix.\n",
    "\n",
    "(b) Show that $H$ also squares to identity.\n",
    "\n",
    "(c) Show that $H P_1 H^\\dagger \\sim P_2$ for Paulis $P_1$ and $P_2$. \n",
    "\n",
    "### Instructions:\n",
    "\n",
    "**Define the Matrices**: In Part 1, define the Hadamard matrix H using NumPy.\n",
    "\n",
    "**Implement the Functions**: In Part 2, complete the function skeletons. Each function is designed to test a specific property of the Hadamard matrix and should return a value that reflects the outcome of the test.\n",
    "\n",
    "Run the Grading: Once you have completed your implementation, run the entire script. The code in the grader will call your functions and use assert statements to check if they are correct.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "# --- Part 1: Define the Matrices ---\n",
    "# Define the Hadamard and Identity matrices as NumPy arrays.\n",
    "\n",
    "H = None  # Replace None with the definition of the Hadamard matrix\n",
    "\n",
    "# --- Part 2: Implement the Test Functions ---\n",
    "# Complete the following functions to test the properties of the Hadamard matrix.\n",
    "\n",
    "def get_hadamard_eigen_system():\n",
    "    \"\"\"\n",
    "    (a) Find the eigenvectors and eigenvalues of the Hadamard matrix.\n",
    "\n",
    "    Returns:\n",
    "        tuple: A tuple of (eigenvalues, eigenvectors)\n",
    "    \"\"\"\n",
    "    # Hint: Use np.linalg.eig() on the Hadamard matrix H. Do it by hand first! \n",
    "    pass  # Your implementation here\n",
    "\n",
    "def test_hadamard_squares_to_identity():\n",
    "    \"\"\"\n",
    "    (b) Show that H squares to the identity matrix.\n",
    "\n",
    "    Returns:\n",
    "        np.array: Result of the calculation asked for\n",
    "    \"\"\"\n",
    "    # Hint: Calculate H @ H and compare the result with I using np.allclose(). \n",
    "    # Do it by hand first!\n",
    "    pass  # Your implementation here\n",
    "\n",
    "def test_hadamard_pauli_transformation():\n",
    "    \"\"\"\n",
    "    (c) Show that conjugating a Pauli with H results in another Pauli.\n",
    "        Specifically, verify:\n",
    "        1. H X H† =?\n",
    "        2. H Y H† = ?\n",
    "        3. H Z H† =?\n",
    "    Returns:\n",
    "        [str1,str2,str3]: strings must be a X Y Z or I and must be written as\n",
    "                          iP, -P, -iP or P, where P is the correct Pauli. \n",
    "    \"\"\"\n",
    "    # Hint: The dagger/conjugate transpose of a matrix M is M.T.conj().\n",
    "    # Do it by hand first!\n",
    "    pass  # Your implementation here\n",
    "### END SOLTUION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test your solutions\n",
    "test_hadamard(H,get_hadamard_eigen_system,test_hadamard_squares_to_identity,test_hadamard_pauli_transformation) # DO NOT EDIT THIS LINE"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
