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
{
"cells": [
{
"cell_type": "markdown",
"id": "neural-latex",
"metadata": {},
"source": [
"# FermionicOp"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "handed-examination",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from qiskit_nature.operators.second_quantization import FermionicOp"
]
},
{
"cell_type": "markdown",
"id": "affiliated-arrangement",
"metadata": {},
"source": [
"## init"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "solved-democrat",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I * 1\n",
"\n",
"+ * 1\n",
"\n",
"- * 1\n",
"\n",
"N * 1\n",
"\n",
"E * 1\n",
"\n"
]
}
],
"source": [
"for l in ['I', '+', '-', 'N', 'E']:\n",
" print(FermionicOp(l))\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "available-aquatic",
"metadata": {},
"outputs": [],
"source": [
"# print(FermionicOp(\"N\", coeff=2)) \n",
"# this initialization is deprecated. Use 2 * FermionicOp(\"N\") alternatevely."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "buried-floating",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" N * 1\n",
"+ E * 1\n"
]
}
],
"source": [
"print(FermionicOp([(\"N\", 1), (\"E\", 1)]))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "possible-guess",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('II', 0))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([], register_length=2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "headed-garbage",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('N', 1), ('E', 3.14)])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"N\", 1), (\"E\", 3.14)]) # __repr__ is evaluatable"
]
},
{
"cell_type": "markdown",
"id": "superb-workplace",
"metadata": {},
"source": [
"## neg, mul, rmul, truediv"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "clinical-vanilla",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('+N-EII', -1))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-FermionicOp(\"+N-EII\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "nearby-resident",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('+-', 2))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp(\"+-\") * 2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "professional-height",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('+N', (6+3j)), ('E-', (2+1j))])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2 + 1j) * FermionicOp([(\"+N\", 3), (\"E-\", 1)])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "complimentary-mercury",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('+N', 1.0), ('E-', 0.3333333333333333)])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"+N\", 3), (\"E-\", 1)]) / 3"
]
},
{
"cell_type": "markdown",
"id": "banned-freeware",
"metadata": {},
"source": [
"## add, sub"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "saving-makeup",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('+N', 3), ('E-', 1)])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 * FermionicOp(\"+N\") + FermionicOp(\"E-\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "royal-generic",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('++', 3), ('--', -2)])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 * FermionicOp(\"++\") - 2 * FermionicOp(\"--\")"
]
},
{
"cell_type": "markdown",
"id": "decent-belarus",
"metadata": {},
"source": [
"## matmul"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "periodic-conflict",
"metadata": {},
"outputs": [],
"source": [
"from itertools import product"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "linear-delight",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I * I\n",
"== I * 1\n",
"\n",
"I * +\n",
"== + * 1\n",
"\n",
"I * -\n",
"== - * 1\n",
"\n",
"I * N\n",
"== N * 1\n",
"\n",
"I * E\n",
"== E * 1\n",
"\n",
"+ * I\n",
"== + * 1\n",
"\n",
"+ * +\n",
"== I * 0\n",
"\n",
"+ * -\n",
"== N * 1\n",
"\n",
"+ * N\n",
"== I * 0\n",
"\n",
"+ * E\n",
"== + * 1\n",
"\n",
"- * I\n",
"== - * 1\n",
"\n",
"- * +\n",
"== E * 1\n",
"\n",
"- * -\n",
"== I * 0\n",
"\n",
"- * N\n",
"== - * 1\n",
"\n",
"- * E\n",
"== I * 0\n",
"\n",
"N * I\n",
"== N * 1\n",
"\n",
"N * +\n",
"== + * 1\n",
"\n",
"N * -\n",
"== I * 0\n",
"\n",
"N * N\n",
"== N * 1\n",
"\n",
"N * E\n",
"== I * 0\n",
"\n",
"E * I\n",
"== E * 1\n",
"\n",
"E * +\n",
"== I * 0\n",
"\n",
"E * -\n",
"== - * 1\n",
"\n",
"E * N\n",
"== I * 0\n",
"\n",
"E * E\n",
"== E * 1\n",
"\n"
]
}
],
"source": [
"for l1, l2 in product(['I', '+', '-', 'N', 'E'], repeat=2):\n",
" print(l1, '*', l2)\n",
" print(\"==\", FermionicOp(l1) @ FermionicOp(l2))\n",
" print()"
]
},
{
"cell_type": "markdown",
"id": "shared-library",
"metadata": {},
"source": [
"$$\n",
"c_1^\\dagger c_0c_1 = - c_1^\\dagger c_1c_0 = -n_1 c_0\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "chemical-spanish",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N- * -1\n"
]
}
],
"source": [
"print(FermionicOp(\"+-\") @ FermionicOp(\"-I\"))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "overhead-mustang",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('+N', 3), ('N+', 3), ('E-', 1), ('-E', -1)])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(3 * FermionicOp(\"+N\") + FermionicOp(\"E-\")) @ (FermionicOp(\"II\") + FermionicOp(\"-+\"))"
]
},
{
"cell_type": "markdown",
"id": "american-camel",
"metadata": {},
"source": [
"## pow"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "technical-atlantic",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('II', 0))"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"+N\", 3), (\"E-\", 1)]) ** 2"
]
},
{
"cell_type": "markdown",
"id": "assured-tumor",
"metadata": {},
"source": [
"$$\n",
"(3c_1^\\dagger c_0^\\dagger c_0 + c_1^\\dagger c_1 c_0)^2\n",
"= 9c_1^\\dagger c_0^\\dagger c_0 c_1^\\dagger c_0^\\dagger c_0 + 3c_1^\\dagger c_0^\\dagger c_0 c_1^\\dagger c_1 c_0 + 3c_1^\\dagger c_1 c_0 c_1^\\dagger c_0^\\dagger c_0 + c_1^\\dagger c_1 c_0 c_1^\\dagger c_1 c_0\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "interim-knock",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('+-', -3))"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"+N\", 3), (\"N-\", 1)]) ** 2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "civil-cursor",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('IIII', 27))"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(3 * FermionicOp(\"IIII\")) ** 3"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "nearby-given",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp(('II', 1))"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"+N\", 3), (\"E-\", 1)]) ** 0"
]
},
{
"cell_type": "markdown",
"id": "personalized-plumbing",
"metadata": {},
"source": [
"## dagger"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "lined-spare",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FermionicOp([('-N', 3), ('N+', 1), ('++', (-2+4j))])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FermionicOp([(\"+N\", 3), (\"N-\", 1), (\"--\", 2 + 4j)]).dagger"
]
},
{
"cell_type": "markdown",
"id": "racial-message",
"metadata": {},
"source": [
"Note that $$\n",
"(c_1c_0)^\\dagger = c_0^\\dagger c_1^\\dagger= - c_1^\\dagger c_0^\\dagger.\n",
"$$"
]
}
],
"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": 5
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment