Skip to content

Instantly share code, notes, and snippets.

@ikkoham
Last active February 3, 2021 04:34
Show Gist options
  • Save ikkoham/3526cbb006af17cb5c97315467e31190 to your computer and use it in GitHub Desktop.
Save ikkoham/3526cbb006af17cb5c97315467e31190 to your computer and use it in GitHub Desktop.
SecondQuantizedOp
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# H2 original style test"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from qiskit.chemistry.drivers import PySCFDriver"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"driver = PySCFDriver(atom='H 0 0 0; H 0 0 1', basis='sto-3g')\n",
"#driver = PySCFDriver(atom='Li 0 0 0; H 0 0 1', basis='sto-3g')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"qmol = driver.run()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-1.11084418e+00 -1.99001544e-16]\n",
" [-2.05161126e-16 -5.89121004e-01]]\n"
]
}
],
"source": [
"print(qmol.mo_onee_ints)\n",
"#print(qmol.one_body_integrals)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[[ 6.26402500e-01 0.00000000e+00]\n",
" [ 5.55111512e-17 6.21706763e-01]]\n",
"\n",
" [[ 9.71445147e-17 1.96790583e-01]\n",
" [ 1.96790583e-01 -2.22044605e-16]]]\n",
"\n",
"\n",
" [[[ 7.63278329e-17 1.96790583e-01]\n",
" [ 1.96790583e-01 0.00000000e+00]]\n",
"\n",
" [[ 6.21706763e-01 0.00000000e+00]\n",
" [-2.77555756e-17 6.53070747e-01]]]]\n"
]
}
],
"source": [
"print(qmol.mo_eri_ints)\n",
"#print(qmol.two_body_integrals)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from qiskit.chemistry.transformations import FermionicTransformation, FermionicQubitMappingType"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"trafo = FermionicTransformation(qubit_mapping=FermionicQubitMappingType.JORDAN_WIGNER)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings(\"ignore\", category=DeprecationWarning)\n",
"qubit_op, _ = trafo.transform(driver)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from qiskit.opflow import PauliSumOp # NOTE: qiskit.opflow.PauliSumOp != qiskit.aqua.PauliSumOp\n",
"qubit_op = PauliSumOp(qubit_op.primitive, qubit_op.coeff)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"IIII\t(-0.8567854005948088+0j)\n",
"IIIZ\t(0.13716572937099503+0j)\n",
"IIZI\t(-0.13036292057109128+0j)\n",
"IIZZ\t(0.10622904490856076+0j)\n",
"IZII\t(0.13716572937099492+0j)\n",
"IZIZ\t(0.15660062488237947+0j)\n",
"IZZI\t(0.15542669077992832+0j)\n",
"XXXX\t(0.04919764587136756+0j)\n",
"XXYY\t(0.04919764587136756+0j)\n",
"YYXX\t(0.04919764587136756+0j)\n",
"YYYY\t(0.04919764587136756+0j)\n",
"ZIII\t(-0.13036292057109128+0j)\n",
"ZIIZ\t(0.15542669077992832+0j)\n",
"ZIZI\t(0.16326768673564354+0j)\n",
"ZZII\t(0.10622904490856076+0j)\n"
]
}
],
"source": [
"for label, coeff in sorted(qubit_op.primitive.to_list()):\n",
" print(f\"{label}\\t{coeff}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# New FermionicOperator test"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"from qiskit_nature.operators.second_quantization import FermionicOp"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" IIII * 0\n",
"+ NIII * -1.110844179883727\n",
"+ INII * -0.5891210037060828\n",
"+ IINI * -1.110844179883727\n",
"+ IIIN * -0.5891210037060828\n",
"+ IIII * 0\n",
"+ NNII * -0.09839529174273512\n",
"+ IIII * 0\n",
"+ NNII * 0.31085338155985665\n",
"+ NINI * 0.31320124976475894\n",
"+ +--+ * -0.09839529174273512\n",
"+ +-+- * 0.09839529174273515\n",
"+ NIIN * 0.31085338155985665\n",
"+ NNII * 0.3108533815598567\n",
"+ IIII * 0\n",
"+ NNII * -0.09839529174273515\n",
"+ IIII * 0\n",
"+ INNI * 0.3108533815598567\n",
"+ -+-+ * 0.09839529174273513\n",
"+ -++- * -0.09839529174273515\n",
"+ ININ * 0.3265353734712871\n",
"+ NINI * 0.31320124976475894\n",
"+ -++- * -0.09839529174273512\n",
"+ +-+- * 0.09839529174273515\n",
"+ INNI * 0.31085338155985665\n",
"+ IIII * 0\n",
"+ IINN * -0.09839529174273512\n",
"+ IIII * 0\n",
"+ IINN * 0.31085338155985665\n",
"+ NIIN * 0.3108533815598567\n",
"+ -+-+ * 0.09839529174273513\n",
"+ +--+ * -0.09839529174273515\n",
"+ ININ * 0.3265353734712871\n",
"+ IINN * 0.3108533815598567\n",
"+ IIII * 0\n",
"+ IINN * -0.09839529174273515\n",
"+ IIII * 0\n",
" +-+- * 0.1967905834854703\n",
"+ +--+ * -0.19679058348547027\n",
"+ -++- * -0.19679058348547027\n",
"+ -+-+ * 0.19679058348547027\n",
"+ IIIN * -0.5891210037060828\n",
"+ IINI * -1.110844179883727\n",
"+ IINN * 0.42491617963424305\n",
"+ INII * -0.5891210037060828\n",
"+ ININ * 0.6530707469425742\n",
"+ INNI * 0.6217067631197133\n",
"+ NIII * -1.110844179883727\n",
"+ NIIN * 0.6217067631197133\n",
"+ NINI * 0.6264024995295179\n",
"+ NNII * 0.42491617963424305\n"
]
}
],
"source": [
"import itertools\n",
"import numpy as np\n",
"\n",
"one_body_ints = qmol.one_body_integrals\n",
"\n",
"register_length = len(one_body_ints)\n",
"fer_op = FermionicOp([], register_length=register_length)\n",
"\n",
"for idx in itertools.product(range(len(one_body_ints)), repeat=2):\n",
" coeff = one_body_ints[idx]\n",
" if not coeff:\n",
" continue\n",
" label = 'I' * register_length\n",
" base_op = FermionicOp(label) * coeff\n",
" for i, op in [(idx[0], '+'), (idx[1], '-')]:\n",
" label_i = label[:i] + op + label[i+1:]\n",
" base_op @= FermionicOp(label_i)\n",
" fer_op += base_op\n",
"\n",
"two_body_ints = qmol.two_body_integrals\n",
"for idx in itertools.product(range(len(two_body_ints)), repeat=4):\n",
" coeff = two_body_ints[idx]\n",
" if not coeff:\n",
" continue\n",
" base_op = FermionicOp(label) * coeff\n",
" for i, op in [(idx[0], '+'), (idx[2], '+'), (idx[3], '-'), (idx[1], '-')]:\n",
" label_i = label[:i] + op + label[i+1:]\n",
" base_op @= FermionicOp(label_i)\n",
" fer_op += base_op\n",
"\n",
"print(fer_op)\n",
"fer_op = fer_op.reduce()\n",
"print(fer_op)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SecondQuantizedOp([FermionicOp([('+-+-', 0.1967905834854703), ('+--+', -0.19679058348547027), ('-++-', -0.19679058348547027), ('-+-+', 0.19679058348547027), ('IIIN', -0.5891210037060828), ('IINI', -1.110844179883727), ('IINN', 0.42491617963424305), ('INII', -0.5891210037060828), ('ININ', 0.6530707469425742), ('INNI', 0.6217067631197133), ('NIII', -1.110844179883727), ('NIIN', 0.6217067631197133), ('NINI', 0.6264024995295179), ('NNII', 0.42491617963424305)])])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from qiskit_nature.operators.second_quantization import SecondQuantizedOp\n",
"second_q_op = SecondQuantizedOp([fer_op])\n",
"second_q_op"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from qiskit_nature.mappings import JordanWignerMapping\n",
"mapping = JordanWignerMapping()\n",
"pauli_op = mapping.map(fer_op)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"IIII\t(-0.8567854005948079+0j)\n",
"IIIZ\t(0.13716572937099503+0j)\n",
"IIZI\t(-0.13036292057109122+0j)\n",
"IIZZ\t(0.10622904490856076+0j)\n",
"IZII\t(0.13716572937099503+0j)\n",
"IZIZ\t(0.15660062488237947+0j)\n",
"IZZI\t(0.15542669077992832+0j)\n",
"XXXX\t(0.049197645871367574+0j)\n",
"XXYY\t(0.049197645871367574+0j)\n",
"YYXX\t(0.049197645871367574+0j)\n",
"YYYY\t(0.049197645871367574+0j)\n",
"ZIII\t(-0.13036292057109122+0j)\n",
"ZIIZ\t(0.15542669077992832+0j)\n",
"ZIZI\t(0.16326768673564354+0j)\n",
"ZZII\t(0.10622904490856076+0j)\n"
]
}
],
"source": [
"for label, coeff in sorted(pauli_op.primitive.to_list()):\n",
" print(f\"{label}\\t{coeff}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 * IIII\n"
]
}
],
"source": [
"diff_op = (qubit_op - pauli_op).reduce()\n",
"print(diff_op)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubit_op == pauli_op"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## to_opflow()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-0.8567854005948079 * IIII\n",
"- 0.13036292057109122 * ZIII\n",
"+ 0.13716572937099503 * IZII\n",
"+ 0.10622904490856076 * ZZII\n",
"- 0.13036292057109122 * IIZI\n",
"+ 0.16326768673564354 * ZIZI\n",
"+ 0.15542669077992832 * IZZI\n",
"+ 0.13716572937099503 * IIIZ\n",
"+ 0.15542669077992832 * ZIIZ\n",
"+ 0.15660062488237947 * IZIZ\n",
"+ 0.10622904490856076 * IIZZ\n",
"+ 0.049197645871367574 * XXXX\n",
"+ 0.049197645871367574 * YYXX\n",
"+ 0.049197645871367574 * XXYY\n",
"+ 0.049197645871367574 * YYYY\n"
]
}
],
"source": [
"print(fer_op.to_opflow())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pauli_op == fer_op.to_opflow()"
]
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment