Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
332 changes: 332 additions & 0 deletions examples/qtensor-vertex-cover.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e54d32e8-4528-4c68-9064-bf60c74673fe",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import networkx as nx\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a211498-211b-4a87-9762-d6c9f6652c4e",
"metadata": {},
"outputs": [],
"source": [
"sys.path.append(\"../\")"
]
},
{
"cell_type": "markdown",
"id": "89452af5-c62b-4e46-bd3a-840adc78ab7c",
"metadata": {},
"source": [
"## Sanity check"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b757515-99d8-44e5-8c53-c370a3b1a331",
"metadata": {},
"outputs": [],
"source": [
"from qtensor import QAOA_energy\n",
"\n",
"G = nx.random_regular_graph(3, 10)\n",
"gamma, beta = [np.pi/3], [np.pi/2]\n",
"\n",
"E = QAOA_energy(G, gamma, beta)"
]
},
{
"cell_type": "markdown",
"id": "5c466f06-511a-483b-8f15-e0aad686bcec",
"metadata": {},
"source": [
"## Vertex Cover: Qiskit"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508eb5b9-782b-4213-a797-5116d847d6b8",
"metadata": {},
"outputs": [],
"source": [
"import qiskit\n",
"qiskit.__qiskit_version__"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3f64065-7a1f-4183-a887-813a235958ee",
"metadata": {},
"outputs": [],
"source": [
"# Qtensor branch: dev\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import networkx as nx\n",
"from qtensor import QAOAComposer\n",
"from qtensor import CirqQAOAComposer, QtreeQAOAComposer\n",
"from qtensor import VCQiskitQAOAComposer, PCQiskitQAOAComposer\n",
"from qtensor import VCQtreeQAOAComposer, PCQtreeQAOAComposer"
]
},
{
"cell_type": "markdown",
"id": "f8b97479-45a4-4b8c-aa64-adda17f1d7eb",
"metadata": {},
"source": [
"### QAOA problem graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dbf5964-25d8-43d8-b11d-7e205dc4cd45",
"metadata": {},
"outputs": [],
"source": [
"\n",
"G = nx.erdos_renyi_graph(4, 2/(5-1))\n",
"nx.draw_kamada_kawai(G, with_labels=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ce1f7b9-49f9-4fe1-8706-495fbad08a09",
"metadata": {},
"outputs": [],
"source": [
"p = 1\n",
"qiskit_qaoa = VCQiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"qiskit_qaoa.ansatz_state()\n",
"qiskit_qaoa.circuit.draw('mpl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d06acf1-f0a7-4b3d-b3e8-2044b9e662b8",
"metadata": {},
"outputs": [],
"source": [
"p = 1\n",
"qiskit_qaoa = PCQiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"qiskit_qaoa.ansatz_state()\n",
"qiskit_qaoa.circuit.draw('mpl')"
]
},
{
"cell_type": "markdown",
"id": "17285e25-e4a8-48f3-830f-819003df454e",
"metadata": {},
"source": [
"## Simulate circuits\n",
"## Use lightcone optimisaiton\n",
"\n",
"Suppose we are interested in an expectation value of particular operator in a state $|\\psi\\rangle = \\hat U | 0\\rangle$. \n",
"We can use the fact that in the expression\n",
"$$\\langle \\psi | \\hat E | \\psi \\rangle = \\langle 0 | \\hat U^\\dagger \\hat E \\hat U |0\\rangle$$\n",
"a lot of operators from $\\hat U$ cancel out."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bacfda45-45de-48c6-84b2-700595abff81",
"metadata": {},
"outputs": [],
"source": [
"p = 2\n",
"qiskit_qaoa = VCQiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"qiskit_qaoa.energy_expectation_lightcone((0,1))\n",
"qiskit_qaoa.circuit.draw('mpl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a96e8f63-c3ee-4918-bc38-bd60f511d305",
"metadata": {},
"outputs": [],
"source": [
"p = 2\n",
"qiskit_qaoa = PCQiskitQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"qiskit_qaoa.energy_expectation_lightcone((0,1))\n",
"qiskit_qaoa.circuit.draw('mpl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0500a31-2cc0-4cba-9169-0c47fc74a969",
"metadata": {},
"outputs": [],
"source": [
"com = VCQtreeQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"com.energy_expectation_lightcone(list(G.edges())[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0473fced-0447-4ecf-ae0d-e964b7511cdb",
"metadata": {},
"outputs": [],
"source": [
"com = PCQtreeQAOAComposer(G, gamma=[.1]*p, beta=[.2]*p)\n",
"com.energy_expectation_lightcone(list(G.edges())[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1da40689-a891-4599-9d75-2ab1bcd6c861",
"metadata": {},
"outputs": [],
"source": [
"from qtensor.QAOASimulator import QAOAQtreeSimulator\n",
"qaoa_sim = QAOAQtreeSimulator(VCQtreeQAOAComposer)\n",
"\n",
"qaoa_sim.energy_expectation(G, gamma=[.1]*p, beta=[.2]*p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79f55af2-a9aa-44f5-beb4-a7180545c6f8",
"metadata": {},
"outputs": [],
"source": [
"from qtensor.QAOASimulator import QAOAQtreeSimulator\n",
"qaoa_sim = QAOAQtreeSimulator(PCQtreeQAOAComposer)\n",
"\n",
"p=3\n",
"qaoa_sim.energy_expectation(G, gamma=[.1]*p, beta=[.2]*p)"
]
},
{
"cell_type": "markdown",
"id": "b158dcfe-404a-4e4b-9654-2d5ee32cde01",
"metadata": {},
"source": [
"## QAOA"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "166e32f4-ddce-45f8-96e5-1bc12ef0a8c8",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.optimize import minimize"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f860402-2c18-463a-8136-8f13e47cc7d7",
"metadata": {},
"outputs": [],
"source": [
"edges = [(0, 1), (1, 2), (2, 0), (2, 3)]\n",
"graph = nx.Graph(edges)\n",
"\n",
"nx.draw(graph, with_labels=True)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d1495c2-fe6b-4b98-89fb-33c1fe8fb196",
"metadata": {},
"outputs": [],
"source": [
"\n",
"p=3\n",
"initial_params = np.array([.1]*2*p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca2cf6ef-7f52-453c-beb6-906fb863afe8",
"metadata": {},
"outputs": [],
"source": [
"def cost_function(params):\n",
" p = len(params) // 2\n",
"\n",
" # Split the single list into two lists\n",
" gammas = params[:p]\n",
" betas = params[p:]\n",
" expectation = qaoa_sim.energy_expectation(graph, gammas, betas)\n",
" return expectation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16de68dc-291a-4bf5-a358-845392b8f9d8",
"metadata": {},
"outputs": [],
"source": [
"# Minimize the function\n",
"result = minimize(cost_function, initial_params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5716d24f-9e74-4e07-afe5-c56ee70f667f",
"metadata": {},
"outputs": [],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7bb1e7c3-285c-4440-8255-7722db98be57",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading