{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Examples using gateraid package" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import relevant modules\n", "\n", "from gateraid import * # To provide examples of the gateraid package, we install all the modules\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using quantum states" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "0+0j|10> + \n", "0.7071+0j|11>\n" ] } ], "source": [ "# Initialise a quantum state\n", "\n", "q = QuantumState(np.array([1, 0, 0, 1])/np.sqrt(2))\n", "print(q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Common two-qubit gates" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Initialise some common gates\n", "\n", "# Control-not gate with control on qubit 0 and target on qubit 1\n", "CX = make_CX()\n", "\n", "# Control-phase gate with anti-control on qubit 1 and target on qubit 0\n", "CZ = make_CZ(False, True)\n", "\n", "# SWAP gate\n", "SWAP = make_SWAP()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "0+0j|10> + \n", "0.7071+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "0.7071+0j|10> + \n", "0+0j|11>\n" ] } ], "source": [ "# Apply the CX gate (expect 1/sqrt(2) (|00> + |11>) --> 1/sqrt(2) (|00> + |10>))\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {CX(q)}')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "0.7071+0j|10> + \n", "0+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "-0.7071+0j|10> + \n", "0+0j|11>\n" ] } ], "source": [ "# Apply the anti-controlled, reversed CZ gate (expect 1/sqrt(2) (|00> + |10>) --> 1/sqrt(2) (|00> - |10>))\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {CZ(q)}')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0+0j|01> + \n", "-0.7071+0j|10> + \n", "0+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "-0.7071+0j|01> + \n", "0+0j|10> + \n", "0+0j|11>\n" ] } ], "source": [ "# Apply the SWAP gate (expect 1/sqrt(2) (|00> - |10>) --> 1/sqrt(2) (|00> - |01>))\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {SWAP(q)}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### General and customised two-qubit gates" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "State: \n", "0.7071+0j|00> + \n", "0.7071+0j|01> + \n", "0+0j|10> + \n", "0+0j|11>\n" ] } ], "source": [ "# Re-initialise the quantum state\n", "\n", "q = QuantumState(np.array([1, 1, 0, 0])/np.sqrt(2))\n", "print(q)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Initialise a local NOT gate acting on qubit 0\n", "\n", "X = make_X()\n", "XI = LocalUnitary(X, 'X', 0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0.7071+0j|00> + \n", "0.7071+0j|01> + \n", "0+0j|10> + \n", "0+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "0+0j|00> + \n", "0+0j|01> + \n", "0.7071+0j|10> + \n", "0.7071+0j|11>\n" ] } ], "source": [ "# Apply the IX gate (expect 1/sqrt(2) (|00> + |01>) --> 1/sqrt(2) (|10> + |11>))\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {XI(q)}')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Initialise a control-Hadamard gate by customising the ControlledUnitary class\n", "\n", "CH = ControlledUnitary(make_H(), 'CH')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0+0j|00> + \n", "0+0j|01> + \n", "0.7071+0j|10> + \n", "0.7071+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "0+0j|00> + \n", "0+0j|01> + \n", "1+0j|10> + \n", "0+0j|11>\n" ] } ], "source": [ "# Apply the CH gate (expect 1/sqrt(2) (|10> + |11>) --> |10>)\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {CH(q)}')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# Initialise a random 2-qubit unitary gate\n", "\n", "coef = np.random.rand(16)\n", "pauli_dict = {'II': coef[0], 'IX': coef[1], 'IY': coef[2], 'IZ': coef[3], 'XI': coef[4], 'XX': coef[5], 'XY': coef[6], 'XZ': coef[7], 'YI': coef[8], 'YX': coef[9], 'YY': coef[10], 'YZ': coef[11], 'ZI': coef[12], 'ZX': coef[13], 'ZY': coef[14], 'ZZ': coef[15]}\n", "\n", "U = GeneralUnitary(pauli_dict)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INITIAL STATE: \n", " State: \n", "0+0j|00> + \n", "0+0j|01> + \n", "1+0j|10> + \n", "0+0j|11> \n", "\n", "FINAL STATE: \n", " State: \n", "-0.04894+0.8219j|00> + \n", "0.1308+0.2922j|01> + \n", "-0.3178-0.3103j|10> + \n", "-0.1481-0.01783j|11>\n" ] } ], "source": [ "# Apply the random gate\n", "\n", "print(f'INITIAL STATE: \\n {q} \\n')\n", "print(f'FINAL STATE: \\n {U(q)}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "qwac_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.4" } }, "nbformat": 4, "nbformat_minor": 2 }