{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MeanHamilMinimizer, native without Autograd\n", "\n", "This notebook is a counterpart to the notebook `MeanHamilMinimizer_native_with_autograd`.\n", "The first problem in both notebooks is identical and done all natively, but here it is done with scipy instead of autograd.\n", "\n", "* Feedback loop between Qubiter and Qubiter\n", "* minimization via scipy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/rrtucci/PycharmProjects/qubiter/qubiter/jupyter_notebooks\n", "/home/rrtucci/PycharmProjects/qubiter\n" ] } ], "source": [ "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": {}, "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 *" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "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": 4, "metadata": {}, "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": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
1
|   Rx
2
|   Ry
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "wr.print_pic_file(jup=True)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "fun_name_to_fun = None" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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": 8, "metadata": {}, "outputs": [], "source": [ "init_var_num_to_rads = {1: .3, 2: .8}\n", "all_var_nums = [1, 2]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "num_samples = 0\n", "print_hiatus = 4\n", "verbose = False\n", "np.random.seed(1234)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "emp_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n", " all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator', 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') # zero samples" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "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": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x_val~ (#1, #2)\n", "iter=0, cost=0.575017, x_val=0.300000, 0.800000\n", "iter=4, cost=-0.597000, x_val=1.300000, 0.800000\n", "iter=8, cost=-0.696599, x_val=1.579575, 0.800000\n", "iter=12, cost=-0.683637, x_val=1.566905, -0.818034\n", "iter=16, cost=-0.999970, x_val=1.566905, 0.000457\n", "iter=20, cost=-0.999970, x_val=1.566905, 0.000457\n", "iter=24, cost=-0.321153, x_val=0.948872, 0.000457\n", "iter=28, cost=-1.000000, x_val=1.570796, 0.000457\n", "iter=32, cost=-0.539918, x_val=1.570796, 1.000457\n", "iter=36, cost=-0.927763, x_val=1.570796, 0.382423\n", "iter=40, cost=-1.000000, x_val=1.570796, 0.000005\n" ] }, { "data": { "text/plain": [ " direc: array([[1., 0.],\n", " [0., 1.]])\n", " fun: -0.9999999999999881\n", " message: 'Optimization terminated successfully.'\n", " nfev: 42\n", " nit: 2\n", " status: 0\n", " success: True\n", " x: array([ 1.57079640e+00, -8.49466221e-11])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mini.find_min(minlib='scipy', method='Powell') " ] } ], "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.10.9" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 4 }