{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a simple quantum circuit using qandle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the required libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# add the parent directory to the path so that we can import the module\n", "import sys\n", "import os\n", "\n", "current_dir = os.getcwd()\n", "sys.path.append(os.path.dirname(os.path.dirname(current_dir)))\n", "sys.path.append(os.path.dirname(current_dir))\n", "import qandle\n", "\n", "qandle.__reimport()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import qandle\n", "import torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define a Quantum Circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circuit = qandle.Circuit(\n", " layers=[\n", " # embedding layer\n", " qandle.AngleEmbedding(qubits=[0, 1], name=\"input0\"),\n", " # trainable layer, with random initialization\n", " qandle.RX(qubit=1),\n", " # trainable layer, with fixed initialization\n", " qandle.RY(qubit=0, theta=torch.tensor(0.2)),\n", " # data reuploading\n", " qandle.RX(qubit=0, name=\"input1\"),\n", " # disable quantum weight remapping\n", " qandle.RY(qubit=1, remapping=None),\n", " qandle.CNOT(control=0, target=1),\n", " qandle.MeasureProbability(),\n", " ]\n", ")\n", "\n", "# print the circuit as a torch module\n", "circuit\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Run the circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_state = torch.rand(circuit.num_qubits, dtype=torch.float) # random input state\n", "input0 = torch.tensor([0.2, 0.1])\n", "input1 = torch.tensor(0.5) \n", "\n", "circuit(input_state, input0=input0, input1=input1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export it to QASM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qasm3 = qandle.convert_to_qasm(circuit, qasm_version=3)\n", "print(qasm3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize the circuit\n", "Use `circuit.draw()` to get a simple string representation of the circuit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(circuit.draw())" ] } ], "metadata": { "kernelspec": { "display_name": "qc2", "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.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }