Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save corba777/e19434f8f808c3c9e2ecd5bcf11ab91c to your computer and use it in GitHub Desktop.

Select an option

Save corba777/e19434f8f808c3c9e2ecd5bcf11ab91c to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pennylane as qml"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from pennylane import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Preparing Q# environment...\n"
]
}
],
"source": [
"dev1=qml.device('microsoft.QuantumSimulator', wires=2, shots=1000)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"@qml.qnode(dev1)\n",
"def circuit(phi,theta):\n",
" qml.RZ(phi,wires=0)\n",
" qml.CNOT(wires=[0,1])\n",
" qml.RY(theta,wires=1)\n",
" return qml.expval(qml.PauliZ(1))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.77"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"circuit(np.pi/4,0.7)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def cost(x,y):\n",
" return np.sin(abs(circuit(x,y)))-1"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"q_grad=qml.grad(cost,argnum=[0,1])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array(-0.00070951), array(-0.45053603))"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q_grad(np.pi/4,0.7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment