{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jorY5_SQA308"
},
"source": [
"# MeanHamilMinimizer, native with Tensorflow\n",
"\n",
" * Feedback loop between Qubiter and Qubiter\n",
" * minimization via tensorflow\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "psPqtcM4BeDO"
},
"source": [
"## First Example (taken from Pennylane docs). "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "4FWT5Lb_pc9Y"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/rrtucci/PycharmProjects/qubiter/qubiter/jupyter_notebooks\n",
"/home/rrtucci/PycharmProjects/qubiter\n"
]
}
],
"source": [
"# use iff running inside qubiter's jupyter-notebooks folder\n",
"\n",
"import os\n",
"import sys\n",
"print(os.getcwd())\n",
"os.chdir('../../')\n",
"print(os.getcwd())\n",
"sys.path.insert(0,os.getcwd())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"executionInfo": {
"elapsed": 1868,
"status": "ok",
"timestamp": 1557782753420,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "fI_HDgItiNAM",
"outputId": "ce2480b9-2432-41b5-c338-5f6b4e695516"
},
"outputs": [],
"source": [
"# use iff running on google colab\n",
"\n",
"# %cd qubiter/"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3322,
"status": "ok",
"timestamp": 1557782754960,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "AuD_-wurBru8",
"outputId": "7dd32bf0-3ed0-49bd-904d-6ae13e7327d6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loaded OneQubitGate, WITHOUT autograd.numpy\n"
]
}
],
"source": [
"from qubiter.adv_applications.MeanHamil_native import *\n",
"from qubiter.adv_applications.MeanHamilMinimizer import *\n",
"import tensorflow as tf\n",
"# tf.enable_eager_execution()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "0pCt2pVSl106"
},
"outputs": [],
"source": [
"num_qbits = 2\n",
"file_prefix = 'mean_hamil_rigetti_test1'\n",
"emb = CktEmbedder(num_qbits, num_qbits)\n",
"wr = SEO_writer(file_prefix, emb)\n",
"wr.write_Rx(0, rads='#1')\n",
"wr.write_Ry(0, rads='-#2*.5')\n",
"wr.close_files()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3262,
"status": "ok",
"timestamp": 1557782755018,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "FhvyBr7rl_i4",
"outputId": "1b2f22ae-dd6c-4e8b-bf43-6030c25304af"
},
"outputs": [
{
"data": {
"text/html": [
"
1 | ROTX\t#1\tAT\t0 |
2 | ROTY\t-#2*.5\tAT\t0 |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wr.print_eng_file(jup=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3198,
"status": "ok",
"timestamp": 1557782755032,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "KFZdBKhLDJwa",
"outputId": "4524f77a-5bea-495f-9227-ce34292e6ffa"
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wr.print_pic_file(jup=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "xRqtpq18mdSQ"
},
"outputs": [],
"source": [
"fun_name_to_fun = None"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3056,
"status": "ok",
"timestamp": 1557782755062,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "PuOFba7GmlmE",
"outputId": "53e82174-3e68-4bda-e939-92bc613a3b07"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hamil=\n",
" 1.0 [Z0]\n"
]
}
],
"source": [
"hamil = QubitOperator('Z0', 1.)\n",
"print('hamil=\\n', hamil)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "rVwpa5XUmsne"
},
"outputs": [],
"source": [
"init_var_num_to_rads = {1: .3, 2: .8}\n",
"all_var_nums = [1, 2]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ZkadskoHmytK"
},
"outputs": [],
"source": [
"num_samples = 0\n",
"print_hiatus = 4\n",
"verbose = False\n",
"np.random.seed(1234)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "L-Vqvj9gm8F0"
},
"outputs": [],
"source": [
"emp_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
" all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator_tf', num_samples=num_samples)\n",
"targ_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
" all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator_tf') # zero samples"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "fJEUs0NEnIDU"
},
"outputs": [],
"source": [
"mini = MeanHamilMinimizer(emp_mhamil, targ_mhamil,\n",
" all_var_nums, init_var_num_to_rads,\n",
" print_hiatus=print_hiatus, verbose=verbose)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 214
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3672,
"status": "ok",
"timestamp": 1557782755954,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "ya7zsvYOnP_E",
"outputId": "c2bb7666-6d75-4ddc-a93d-57ca93a28a65"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_val~ (#1, #2)\n",
"iter=0, cost=0.575017, targ_cost=0.575017, x_val=0.300000, 0.800000\n",
"iter=4, cost=0.123857, targ_cost=0.123857, x_val=0.670501, 0.995876\n",
"iter=8, cost=-0.319743, targ_cost=-0.319743, x_val=1.084728, 0.967467\n",
"iter=12, cost=-0.697391, targ_cost=-0.697391, x_val=1.400917, 0.738210\n",
"iter=16, cost=-0.872896, targ_cost=-0.872896, x_val=1.533648, 0.504718\n",
"iter=20, cost=-0.944003, targ_cost=-0.944003, x_val=1.564640, 0.336021\n",
"iter=24, cost=-0.975490, targ_cost=-0.975490, x_val=1.569905, 0.221851\n",
"iter=28, cost=-0.989368, targ_cost=-0.989368, x_val=1.570675, 0.145955\n",
"iter=32, cost=-0.995408, targ_cost=-0.995408, x_val=1.570780, 0.095874\n",
"iter=36, cost=-0.998020, targ_cost=-0.998020, x_val=1.570794, 0.062935\n"
]
}
],
"source": [
"mini.find_min(minlib='tflow', num_iter=40, descent_rate=.1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "oi9ffqrCC1R2"
},
"source": [
"## Second, more complicated example"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "pvha-3rPCxM4"
},
"outputs": [],
"source": [
"num_qbits = 4\n",
"file_prefix = 'mean_hamil_rigetti_test2'\n",
"emb = CktEmbedder(num_qbits, num_qbits)\n",
"wr = SEO_writer(file_prefix, emb)\n",
"wr.write_Ry(2, rads=np.pi/7)\n",
"wr.write_Ry(1, rads='#2')\n",
"wr.write_Rx(1, rads='#1')\n",
"wr.write_cnot(2, 3)\n",
"wr.write_qbit_swap(1, 2)\n",
"wr.close_files()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3548,
"status": "ok",
"timestamp": 1557782755976,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "2IWQMSUMDBfc",
"outputId": "65db8f07-21ca-4864-89df-0cbfc61a8ebe"
},
"outputs": [
{
"data": {
"text/html": [
"1 | ROTY\t25.714286\tAT\t2 |
2 | ROTY\t#2\tAT\t1 | 3 | ROTX\t#1\tAT\t1 | 4 | SIGX\tAT\t3\tIF\t2T | 5 | SWAP\t2\t1 |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wr.print_eng_file(jup=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 124
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3484,
"status": "ok",
"timestamp": 1557782755982,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "Q9uK8C2aDOiu",
"outputId": "3771ecdb-60bc-4d2d-d41f-51e090a82e2b"
},
"outputs": [
{
"data": {
"text/html": [
"1 | | Ry | | |
2 | | | Ry | | 3 | | | Rx | | 4 | X---@ | | | 5 | | <---> | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"wr.print_pic_file(jup=True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "RA2mnqdUDV6W"
},
"outputs": [],
"source": [
"fun_name_to_fun = None"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
},
"colab_type": "code",
"executionInfo": {
"elapsed": 3392,
"status": "ok",
"timestamp": 1557782755998,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "yQSFvUmoDYeq",
"outputId": "98842783-6cbd-4c43-b9a4-53db7b3e72a2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hamil=\n",
" 0.7 [X1 Y2] +\n",
"0.4 [Y1 X2 Y3]\n"
]
}
],
"source": [
"hamil = QubitOperator('X1 Y3 X1 Y1 X2', .4) + QubitOperator('Y2 X1', .7)\n",
"print('hamil=\\n', hamil)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "zyBs3UWbDdp8"
},
"outputs": [],
"source": [
"init_var_num_to_rads = {1: 2.1, 2:1.2}\n",
"all_var_nums = [1, 2]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "L3ZhoZioDi4-"
},
"outputs": [],
"source": [
"num_samples = 0\n",
"print_hiatus = 2\n",
"verbose = False\n",
"np.random.seed(1234)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "0NkkO_-oDn5-"
},
"outputs": [],
"source": [
"emp_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
" all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator_tf', num_samples=num_samples)\n",
"targ_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
" all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator_tf') # zero samples"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "QDZXyKrJDtGs"
},
"outputs": [],
"source": [
"mini = MeanHamilMinimizer(emp_mhamil, targ_mhamil,\n",
" all_var_nums, init_var_num_to_rads,\n",
" print_hiatus=print_hiatus, verbose=verbose)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 214
},
"colab_type": "code",
"executionInfo": {
"elapsed": 4094,
"status": "ok",
"timestamp": 1557782756898,
"user": {
"displayName": "Robert Tucci",
"photoUrl": "",
"userId": "06804474810563551288"
},
"user_tz": 240
},
"id": "r5XvrEQoDxzm",
"outputId": "a1772486-2bd8-4975-ebdb-f3e2ef86f56f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_val~ (#1, #2)\n",
"iter=0, cost=-0.211239, targ_cost=-0.211239, x_val=2.100000, 1.200000\n",
"iter=2, cost=-0.248413, targ_cost=-0.248413, x_val=2.100000, 1.111845\n",
"iter=4, cost=-0.273138, targ_cost=-0.273138, x_val=2.100000, 1.039734\n",
"iter=6, cost=-0.288820, targ_cost=-0.288820, x_val=2.100000, 0.982194\n",
"iter=8, cost=-0.298464, targ_cost=-0.298464, x_val=2.100000, 0.937018\n",
"iter=10, cost=-0.304281, targ_cost=-0.304281, x_val=2.100000, 0.901905\n",
"iter=12, cost=-0.307749, targ_cost=-0.307749, x_val=2.100000, 0.874784\n",
"iter=14, cost=-0.309801, targ_cost=-0.309801, x_val=2.100000, 0.853911\n",
"iter=16, cost=-0.311011, targ_cost=-0.311011, x_val=2.100000, 0.837884\n",
"iter=18, cost=-0.311723, targ_cost=-0.311723, x_val=2.100000, 0.825593\n"
]
}
],
"source": [
"mini.find_min(minlib='tflow', num_iter=20, descent_rate=.1)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "MeanHamilMinimizer_native_with_tf.ipynb",
"provenance": [],
"version": "0.3.2"
},
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}