Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Miao-JQ authored Aug 8, 2023
1 parent 46a16fa commit 607fe0e
Showing 1 changed file with 63 additions and 49 deletions.
112 changes: 63 additions & 49 deletions docs/source/tutorials/nnvqe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
" n = c._nqubits\n",
" for i in range(n):\n",
" e += lamb * c.expectation((tc.gates.z(), [i])) # <Z_i>\n",
" for i in range(n): \n",
" for i in range(n):\n",
" e += c.expectation(\n",
" (tc.gates.x(), [i]), (tc.gates.x(), [(i + 1) % n])\n",
" ) # <X_i X_{i+1}>\n",
Expand Down Expand Up @@ -115,32 +115,33 @@
"metadata": {},
"outputs": [],
"source": [
"def MERA(inp, n, d=1, lamb=1., energy_flag=False): # for single-parameter 1D XXZ model, we fix lamb\n",
"def MERA(\n",
" inp, n, d=1, lamb=1.0, energy_flag=False\n",
"): # for single-parameter 1D XXZ model, we fix lamb\n",
" params = K.cast(inp[\"params\"], \"complex128\")\n",
" delta = K.cast(inp[\"delta\"], \"complex128\") \n",
" delta = K.cast(inp[\"delta\"], \"complex128\")\n",
" c = tc.Circuit(n)\n",
" \n",
"\n",
" idx = 0\n",
"\n",
" for i in range(n):\n",
" c.rx(i, theta=params[3 * i])\n",
" c.rz(i, theta=params[3 * i + 1])\n",
" c.rx(i, theta=params[3 * i + 2])\n",
" idx += 3 * n\n",
" \n",
"\n",
" for n_layer in range(1, int(np.log2(n)) + 1):\n",
" n_qubit = 2**n_layer # number of qubits involving\n",
" n_qubit = 2**n_layer # number of qubits involving\n",
" step = int(n / n_qubit)\n",
"\n",
" for _ in range(d): # circuit depth\n",
" # even \n",
" for _ in range(d): # circuit depth\n",
" # even\n",
" for i in range(step, n - step, 2 * step):\n",
" c.rxx(i, i + step, theta=params[idx])\n",
" c.rzz(i, i + step, theta=params[idx + 1])\n",
" idx += 2\n",
" \n",
" # odd \n",
"\n",
" # odd\n",
" for i in range(0, n, 2 * step):\n",
" c.rxx(i, i + step, theta=params[idx])\n",
" c.rzz(i, i + step, theta=params[idx + 1])\n",
Expand All @@ -151,11 +152,11 @@
" c.rx(i, theta=params[idx])\n",
" c.rz(i, theta=params[idx + 1])\n",
" idx += 2\n",
" \n",
"\n",
" if energy_flag:\n",
" return energy(c, lamb, delta) # return Hamiltonian energy expectation\n",
" return energy(c, lamb, delta) # return Hamiltonian energy expectation\n",
" else:\n",
" return c, idx # return the circuit & number of circuit parameters"
" return c, idx # return the circuit & number of circuit parameters"
]
},
{
Expand Down Expand Up @@ -2895,7 +2896,7 @@
"# circuit visulization\n",
"n = 8\n",
"d = 1\n",
"cirq, idx = MERA({\"params\": np.zeros(3000), \"delta\": 0.}, n, d, 1.)\n",
"cirq, idx = MERA({\"params\": np.zeros(3000), \"delta\": 0.0}, n, d, 1.0)\n",
"print(\"The number of parameters is\", idx)\n",
"cirq.draw()"
]
Expand All @@ -2919,26 +2920,32 @@
"outputs": [],
"source": [
"def NN_MERA(n, d, lamb, NN_shape, stddev):\n",
" input = tf.keras.layers.Input(shape=[1]) # input layer\n",
" input = tf.keras.layers.Input(shape=[1]) # input layer\n",
"\n",
" x = tf.keras.layers.Dense(\n",
" units=NN_shape, \n",
" units=NN_shape,\n",
" kernel_initializer=tf.keras.initializers.RandomNormal(stddev=stddev),\n",
" activation=\"ReLU\"\n",
" )(input) # hidden layer\n",
" activation=\"ReLU\",\n",
" )(\n",
" input\n",
" ) # hidden layer\n",
"\n",
" x = tf.keras.layers.Dropout(0.05)(x) # dropout layer\n",
" x = tf.keras.layers.Dropout(0.05)(x) # dropout layer\n",
"\n",
" _, idx = MERA({\"params\": np.zeros(3000), \"delta\": 0.}, n, d, 1., energy_flag=False)\n",
" _, idx = MERA(\n",
" {\"params\": np.zeros(3000), \"delta\": 0.0}, n, d, 1.0, energy_flag=False\n",
" )\n",
" params = tf.keras.layers.Dense(\n",
" units=idx, \n",
" kernel_initializer=tf.keras.initializers.RandomNormal(stddev=stddev),\n",
" activation=\"sigmoid\"\n",
" )(x) # output layer\n",
" \n",
" qlayer = tc.KerasLayer(partial(MERA, n=n, d=d, lamb=lamb, energy_flag=True)) # PQC\n",
" units=idx,\n",
" kernel_initializer=tf.keras.initializers.RandomNormal(stddev=stddev),\n",
" activation=\"sigmoid\",\n",
" )(\n",
" x\n",
" ) # output layer\n",
"\n",
" qlayer = tc.KerasLayer(partial(MERA, n=n, d=d, lamb=lamb, energy_flag=True)) # PQC\n",
"\n",
" output = qlayer({\"params\": 6.3 * params, \"delta\": input}) # NN-VQE output\n",
" output = qlayer({\"params\": 6.3 * params, \"delta\": input}) # NN-VQE output\n",
"\n",
" m = tf.keras.Model(inputs=input, outputs=output)\n",
"\n",
Expand All @@ -2965,24 +2972,24 @@
},
"outputs": [],
"source": [
"def train(n, d, lamb, delta, NN_shape, maxiter=10000, lr=0.005, stddev=1.):\n",
"def train(n, d, lamb, delta, NN_shape, maxiter=10000, lr=0.005, stddev=1.0):\n",
" exp_lr = tf.keras.optimizers.schedules.ExponentialDecay(\n",
" initial_learning_rate=lr, decay_steps=1000, decay_rate=0.7\n",
" )\n",
" opt = tf.keras.optimizers.Adam(exp_lr) # optimizer\n",
" initial_learning_rate=lr, decay_steps=1000, decay_rate=0.7\n",
" )\n",
" opt = tf.keras.optimizers.Adam(exp_lr) # optimizer\n",
"\n",
" m = NN_MERA(n, d, lamb, NN_shape, stddev)\n",
" for i in range(maxiter):\n",
" with tf.GradientTape() as tape:\n",
" e = tf.zeros([1], dtype=tf.float64)\n",
" for de in delta:\n",
" e += m(K.reshape(de, [1])) # sum up energies of all training points\n",
" e += m(K.reshape(de, [1])) # sum up energies of all training points\n",
" grads = tape.gradient(e, m.variables)\n",
" opt.apply_gradients(zip(grads, m.variables))\n",
" if i % 500 == 0:\n",
" print(\"epoch\", i, \":\", e)\n",
" \n",
" m.save_weights('NN-VQE.weights.h5') # save the trained model"
"\n",
" m.save_weights(\"NN-VQE.weights.h5\") # save the trained model"
]
},
{
Expand All @@ -3006,16 +3013,16 @@
}
],
"source": [
"n = 8 # number of qubits\n",
"d = 2 # circuit depth\n",
"lamb = 0.75 # fixed\n",
"delta = np.linspace(-3.0, 3.0, 20, dtype=\"complex128\") # training set\n",
"NN_shape = 20 # node number of the hidden layer\n",
"maxiter = 2500 # maximum iteration for the optimization\n",
"lr = 0.009 # learning rate\n",
"stddev = 0.1 # the initial standard deviation of the NN\n",
"n = 8 # number of qubits\n",
"d = 2 # circuit depth\n",
"lamb = 0.75 # fixed\n",
"delta = np.linspace(-3.0, 3.0, 20, dtype=\"complex128\") # training set\n",
"NN_shape = 20 # node number of the hidden layer\n",
"maxiter = 2500 # maximum iteration for the optimization\n",
"lr = 0.009 # learning rate\n",
"stddev = 0.1 # the initial standard deviation of the NN\n",
"\n",
"with tf.device('/cpu:0'):\n",
"with tf.device(\"/cpu:0\"):\n",
" train(n, d, lamb, delta, NN_shape=NN_shape, maxiter=maxiter, lr=lr, stddev=stddev)"
]
},
Expand Down Expand Up @@ -3052,10 +3059,10 @@
}
],
"source": [
"test_delta = np.linspace(-4.0, 4.0, 201) # test set\n",
"test_delta = np.linspace(-4.0, 4.0, 201) # test set\n",
"test_energies = tf.zeros_like(test_delta).numpy()\n",
"m = NN_MERA(n, d, lamb, NN_shape, stddev)\n",
"m.load_weights('DNN-MERA_2[20](-3.0,3.0,20)_drop05.weights.h5')\n",
"m.load_weights(\"DNN-MERA_2[20](-3.0,3.0,20)_drop05.weights.h5\")\n",
"for i, de in tqdm(enumerate(test_delta)):\n",
" test_energies[i] = m(K.reshape(de, [1]))"
]
Expand All @@ -3077,9 +3084,11 @@
"metadata": {},
"outputs": [],
"source": [
"analytical_energies = [] # analytical result\n",
"analytical_energies = [] # analytical result\n",
"for i in test_delta:\n",
" h = quimb.tensor.tensor_builder.MPO_ham_XXZ(n, i*4, jxy=4., bz=2.*0.75, S=0.5, cyclic=True) \n",
" h = quimb.tensor.tensor_builder.MPO_ham_XXZ(\n",
" n, i * 4, jxy=4.0, bz=2.0 * 0.75, S=0.5, cyclic=True\n",
" )\n",
" h = h.to_dense()\n",
" analytical_energies.append(np.min(quimb.eigvalsh(h)))"
]
Expand Down Expand Up @@ -3956,10 +3965,15 @@
],
"source": [
"# relative error\n",
"plt.plot(test_delta, (test_energies - analytical_energies) / np.abs(analytical_energies), '-', color='b')\n",
"plt.plot(\n",
" test_delta,\n",
" (test_energies - analytical_energies) / np.abs(analytical_energies),\n",
" \"-\",\n",
" color=\"b\",\n",
")\n",
"plt.xlabel(\"Delta\", fontsize=14)\n",
"plt.ylabel(\"GS Relative Error\", fontsize=14)\n",
"plt.axvspan(-3.0, 3.0, color='darkgrey', alpha=0.5) # training set span\n",
"plt.axvspan(-3.0, 3.0, color=\"darkgrey\", alpha=0.5) # training set span\n",
"plt.show()"
]
},
Expand Down

0 comments on commit 607fe0e

Please sign in to comment.