{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Emulating custom hardware inside universal processors\n", "\n", "In order to correctly emulate the behavior of a software stack built on a specific hardware, one might need to force some simulator to publish some particular hardware specs.\n", "\n", "Lets say, for instance, that you want to particularize the `LinAlg` simulator in order for it to publish a limited connectivity, or a fixed number of qubits. Any plugin on top of this particularized `LinAlg` would see these constraints, and would act according to this information.\n", "\n", "This is exactly the purpose of the `Quameleon` Plugin.\n", "\n", "The following cell demonstrate the use of this Plugin to build a custom `LinAlg` instance that publish a limited number of qubits over a linear nearest neighbor connectivity." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.core import HardwareSpecs, Topology, TopologyType\n", "from qat.core.quameleon import QuameleonPlugin\n", "from qat.qpus import get_default_qpu\n", "\n", "my_custom_specs = HardwareSpecs(nbqbits=12, topology=Topology(type=TopologyType.LNN))\n", "\n", "qpu = QuameleonPlugin(specs=my_custom_specs) | get_default_qpu()\n", "\n", "qpu_specs = qpu.get_specs()\n", "print(\"Default specs of LinAlg:\", get_default_qpu().get_specs())\n", "print(\"Our specs:\", qpu_specs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, this new QPU publishes exactly the specs we required, instead of publishing a trivial set of specs.\n", "\n", "Moreover, sending a circuit that is not compliant with these specs will raise an appropriate exception:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.lang.AQASM import *\n", "\n", "prog = Program()\n", "qbits= prog.qalloc(3)\n", "prog.apply(CNOT, qbits[0], qbits[2])# This gate is not LNN\n", "job = prog.to_circ().to_job()\n", "try:\n", " qpu.submit(job)\n", "except Exception as e:\n", " print(e.message)\n", " \n", "prog = Program()\n", "qbits= prog.qalloc(15)\n", "prog.apply(H, qbits[14])# This qubit is out of bound\n", "job = prog.to_circ().to_job()\n", "try:\n", " qpu.submit(job)\n", "except Exception as e:\n", " print(e.message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, we can do the same with custom topologies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_topology = Topology(type=TopologyType.CUSTOM)\n", "my_topology.add_edge(0, 2)\n", "my_topology.add_edge(1, 2)\n", "my_topology.add_edge(3, 2)\n", "my_topology.add_edge(4, 2)\n", "my_topology.add_edge(0, 1)\n", "my_topology.add_edge(3, 4)\n", "\n", "my_ibm_qx4_specs = HardwareSpecs(nbqbits=5, topology=my_topology)\n", "\n", "qpu = QuameleonPlugin(specs=my_ibm_qx4_specs) | get_default_qpu()\n", "print(qpu.get_specs())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And also custom gate sets:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.core.gate_set import GateSet\n", "\n", "gate_set = GateSet({\n", " \"H\" : AbstractGate(\"H\", [], arity=1)\n", "})\n", "my_specs = HardwareSpecs(gateset=gate_set)\n", "\n", "\n", "qpu = QuameleonPlugin(specs=my_specs) | get_default_qpu()\n", "print(qpu.get_specs())\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# declaring a custom gate \"FOO\"\n", "FOO = AbstractGate(\"FOO\", [], arity=1)\n", "prog = Program()\n", "qbits= prog.qalloc(1)\n", "prog.apply(FOO(), qbits)\n", "job = prog.to_circ().to_job()\n", "try:\n", " qpu.submit(job)\n", "except Exception as e:\n", " print(e)\n", "# using only declared gates\n", "prog = Program()\n", "qbits= prog.qalloc(1)\n", "prog.apply(H, qbits)\n", "job = prog.to_circ().to_job()\n", "results = qpu.submit(job)\n", "print(\"Everything went fine.\")" ] } ], "metadata": { "authors": [ "Simon Martiel" ], "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }