Skip to content

Commit

Permalink
FT LSP task
Browse files Browse the repository at this point in the history
  • Loading branch information
remmyzen committed Feb 12, 2024
1 parent 5b18483 commit db82952
Show file tree
Hide file tree
Showing 5 changed files with 1,504 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,382 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b5b1fb49-9cb4-4c8e-a87c-103f9944bbd3",
"metadata": {},
"source": [
"# Integrated Fault-Tolerant Logical State Preparation\n",
"\n",
"Example notebook for integrated fault-tolerant logical state preparation task."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6be8e31-a221-4b56-b744-b13d1d33c5fc",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(r'../')\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"id": "21b2a79f-b07d-45e1-8984-da5df2733def",
"metadata": {
"tags": []
},
"source": [
"## 01 - Simplest Example\n",
"\n",
"In the simplest example, you only need to specify the target stabilizers of your logical state.\n",
"\n",
"By default, [CX, CZ, S, H] gate set and all-to-all qubit connectivity will be used."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "010abcd4-6e5c-466c-8aae-c1a35dd78953",
"metadata": {},
"outputs": [],
"source": [
"from rlftqc.ft_logical_state_preparation import FTLogicalStatePreparation\n",
"\n",
"## Define the target stabilizers\n",
"## For example, zero logical of 7 qubit Steane code.\n",
"target = [\"+ZZZZZZZ\",\n",
" \"+ZIZIZIZ\",\n",
" \"+XIXIXIX\",\n",
" \"+IZZIIZZ\",\n",
" \"+IXXIIXX\",\n",
" \"+IIIZZZZ\",\n",
" \"+IIIXXXX\",\n",
" ]\n",
"\n",
"## Create class\n",
"ftlsp = FTLogicalStatePreparation(target, ignore_z_errors=True)\n"
]
},
{
"cell_type": "markdown",
"id": "34e6af2e-7121-498f-bbfc-f90619830dd1",
"metadata": {},
"source": [
"We now train the agent. It takes around 100 seconds to train. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97cf3648-f16d-4016-863b-8abec67d1185",
"metadata": {},
"outputs": [],
"source": [
"## Train the agent\n",
"ftlsp.train()"
]
},
{
"cell_type": "markdown",
"id": "bed84624-e9fd-4558-9ba8-6cb70220613a",
"metadata": {},
"source": [
"After the training is done, we can now the run the agent to get the circuit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67e00217-a5a4-45f5-bac9-6e3ae09bc284",
"metadata": {},
"outputs": [],
"source": [
"ftlsp.run()"
]
},
{
"cell_type": "markdown",
"id": "7339142f-dba6-4f92-be3a-a7e4e6ec619b",
"metadata": {},
"source": [
"We can also customize the folder name to save the circuit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "46bacd6a-3c4d-4785-9117-302ae0b173d4",
"metadata": {},
"outputs": [],
"source": [
"ftlsp.run(results_folder_name='results')"
]
},
{
"cell_type": "markdown",
"id": "61b527a5-7b1e-40c2-b170-c0c4326d3c56",
"metadata": {},
"source": [
"We can also log the result to check the training convergence."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a64686c-aad1-48c7-8ff6-1eb326bb3716",
"metadata": {},
"outputs": [],
"source": [
"## Log the result if needed\n",
"ftlsp.log()"
]
},
{
"cell_type": "markdown",
"id": "db5d8284-cc41-4a56-9d4c-3dd0adff84c8",
"metadata": {},
"source": [
"We can also customize the folder name to log the experiment."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5c905ab-e58d-46d6-8533-78c0c5a3f873",
"metadata": {},
"outputs": [],
"source": [
"## One can also customize the folder name to save log\n",
"ftlsp.log(results_folder_name='logs')"
]
},
{
"cell_type": "markdown",
"id": "26ee4805-ac59-497e-8893-5551c0991f2e",
"metadata": {},
"source": [
"## 02 - Different Gate Set and Qubit Connectivity\n",
"\n",
"In this part, we specify a different gate set and qubit connectivity.\n",
"We try with IBM native gate set: [CX, S, SQRT_X, and X] and next-nearest neighbors qubit connectivity."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63b30d72-d7c7-4f5b-ac28-8ce70f2cf86d",
"metadata": {},
"outputs": [],
"source": [
"from rlftqc.logical_state_preparation import LogicalStatePreparation\n",
"from rlftqc.simulators.clifford_gates import CliffordGates\n",
"\n",
"## Define the target stabilizers\n",
"## For example, zero logical of 5 qubit perfect code.\n",
"target = [\n",
" \"+ZZZZZ\",\n",
" \"+IXZZX\",\n",
" \"+XZZXI\",\n",
" \"+ZZXIX\",\n",
" \"+ZXIXZ\"]\n",
"\n",
"## Specify gates\n",
"cliff_gates = CliffordGates(5)\n",
"gates = [cliff_gates.s, cliff_gates.cx, cliff_gates.sqrt_x, cliff_gates.x]\n",
"\n",
"## Create next-nearest neighbors connectivity graph\n",
"graph = []\n",
"for ii in range(4):\n",
" graph.append((ii, ii+1))\n",
" graph.append((ii+1, ii))\n",
"print(graph)\n",
" \n",
"## Create class\n",
"lsp = LogicalStatePreparation(target, gates=gates, graph=graph)"
]
},
{
"cell_type": "markdown",
"id": "1574325c-644d-4636-a319-44daac85a641",
"metadata": {},
"source": [
"We now train the agent. It takes around 60 seconds to train. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35267c81-8167-4771-a458-58c891331ff8",
"metadata": {},
"outputs": [],
"source": [
"lsp.train()"
]
},
{
"cell_type": "markdown",
"id": "c809ca77-a608-4b37-bd08-cd8a48e2b51e",
"metadata": {},
"source": [
"Run the agent and get the prepared circuit"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec17c2ec-b288-4e90-84bb-9cca5c9d0f71",
"metadata": {},
"outputs": [],
"source": [
"lsp.run()"
]
},
{
"cell_type": "markdown",
"id": "dcd0b753-64c0-4031-abf9-e8cabc0dbadd",
"metadata": {},
"source": [
"We can also log the result to check the training convergence."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e5110f6-ff4f-4f48-a87a-596ef3f6b90b",
"metadata": {},
"outputs": [],
"source": [
"lsp.log()"
]
},
{
"cell_type": "markdown",
"id": "5e701304-a719-4fc1-895f-6fc349d5666b",
"metadata": {},
"source": [
"## 03 - Advanced Example \n",
"\n",
"This part shows how to customize the training configuration."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66d471ba-dbd5-4ea2-abf7-94d806f50b09",
"metadata": {},
"outputs": [],
"source": [
"from rlftqc.logical_state_preparation import LogicalStatePreparation\n",
"\n",
"## Define the target stabilizers\n",
"## For example, zero logical of 7 qubit Steane code.\n",
"target = [\"ZZZZZZZ\",\n",
" \"ZIZIZIZ\",\n",
" \"XIXIXIX\",\n",
" \"IZZIIZZ\",\n",
" \"IXXIIXX\",\n",
" \"IIIZZZZ\",\n",
" \"IIIXXXX\",\n",
" ]\n",
"\n",
"## Create class\n",
"lsp = LogicalStatePreparation(target)\n"
]
},
{
"cell_type": "markdown",
"id": "c724948a-6fdf-4177-82b5-3ad7de6ac3b1",
"metadata": {},
"source": [
"Change the number of possible gates for training with the max_steps."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44286fcf-cd66-4483-abd5-91553fa82e44",
"metadata": {},
"outputs": [],
"source": [
"lsp = LogicalStatePreparation(target, max_steps = 100)"
]
},
{
"cell_type": "markdown",
"id": "31a9429b-c791-4019-86cc-2693b5808c82",
"metadata": {},
"source": [
"Change seed for training."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef558728-d15f-4796-9e44-6895888630ba",
"metadata": {},
"outputs": [],
"source": [
"lsp = LogicalStatePreparation(target, seed = 123)"
]
},
{
"cell_type": "markdown",
"id": "538841d6-f720-4a13-bec2-acf25205f00c",
"metadata": {},
"source": [
"For more advanced training configurations, we can change the training config."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "760f5a6d-789a-4b91-ba13-91d9be0365ce",
"metadata": {},
"outputs": [],
"source": [
"lsp.training_config"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "314535ff-127e-4330-9704-c2896f71115b",
"metadata": {},
"outputs": [],
"source": [
"# NUM_AGENTS change the number of parallel agents to train (default: 1).\n",
"lsp.training_config['NUM_AGENTS'] = 5\n",
"\n",
"# TOTAL_TIMESTEPS change the number of total timesteps for training (default: 5e5), increase this for longer training.\n",
"lsp.training_config['TOTAL_TIMESTEPS'] = 1e7\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Jax",
"language": "python",
"name": "jax"
},
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 2 additions & 0 deletions rlftqc/envs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rlftqc.envs.logical_state_preparation_env import LogicalStatePreparationEnv
from rlftqc.envs.verification_circuit_synthesis_env import VerificationCircuitSynthesisEnv
from rlftqc.envs.ft_logical_state_preparation_env import FTLogicalStatePreparationEnv


Loading

0 comments on commit db82952

Please sign in to comment.