Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Diagonal operations handling in HamiltonianEvolution #282

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 307 additions & 0 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3517b062-6445-4502-8c92-6a486a55166d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"PyQTorch logger successfully setup with log level 20\n"
]
}
],
"source": [
"import pyqtorch as pyq\n",
"from pyqtorch import product_state\n",
"from pyqtorch.matrices import parametric_unitary, add_batch_dim\n",
"from pyqtorch.primitives import Primitive\n",
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "30b5210d-3c7c-4dad-9676-5faf8138c70f",
"metadata": {},
"outputs": [],
"source": [
"state_111 = product_state(\"11\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "263a1ed0-36d9-48ab-92e4-b8ebdff352bd",
"metadata": {},
"outputs": [],
"source": [
"# torch.diag_embed(czop_example.tensor().squeeze(-1))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3f5d8bf0-98f8-4b07-b98c-9f3229c2ec4d",
"metadata": {},
"outputs": [],
"source": [
"# torch.diag_embed(torch.concatenate([czop_example.tensor(), czop_example.tensor(), czop_example.tensor()], axis=-1))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "11d6a6b5-5b50-447e-9a1c-58e864ec8656",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[[ 2.+0.j],\n",
" [ 0.+0.j],\n",
" [ 0.+0.j],\n",
" [ 0.+0.j]],\n",
"\n",
" [[ 0.+0.j],\n",
" [ 2.+0.j],\n",
" [ 0.+0.j],\n",
" [ 0.+0.j]],\n",
"\n",
" [[ 0.+0.j],\n",
" [ 0.+0.j],\n",
" [-1.+0.j],\n",
" [ 1.+0.j]],\n",
"\n",
" [[ 0.+0.j],\n",
" [ 0.+0.j],\n",
" [ 1.+0.j],\n",
" [-1.+0.j]]], dtype=torch.complex128)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#czop_seq = pyq.Sequence([pyq.X(0), pyq.CNOT(control=0, target=1), czop_example, ])\n",
"czop_seq = pyq.Add([pyq.CNOT(control=0, target=1), pyq.Z(0, diagonal=False), ])\n",
"czop_seq.tensor()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "643905c7-6ee0-48a6-8b59-7c6d241256c1",
"metadata": {},
"outputs": [
{
"ename": "RuntimeError",
"evalue": "einsum(): subscript i has size 2 for operand 1 which does not broadcast with previously seen size 4",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[25], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43meinsum\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mijb,ikb->ijb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpyq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCNOT\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcontrol\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpyq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mZ\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdiagonal\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43munsqueeze\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Library/Application Support/hatch/env/virtual/pyqtorch/Gy8xvlfd/pyqtorch/lib/python3.10/site-packages/torch/functional.py:385\u001b[0m, in \u001b[0;36meinsum\u001b[0;34m(*args)\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m einsum(equation, \u001b[38;5;241m*\u001b[39m_operands)\n\u001b[1;32m 382\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(operands) \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m opt_einsum\u001b[38;5;241m.\u001b[39menabled:\n\u001b[1;32m 383\u001b[0m \u001b[38;5;66;03m# the path for contracting 0 or 1 time(s) is already optimized\u001b[39;00m\n\u001b[1;32m 384\u001b[0m \u001b[38;5;66;03m# or the user has disabled using opt_einsum\u001b[39;00m\n\u001b[0;32m--> 385\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_VF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43meinsum\u001b[49m\u001b[43m(\u001b[49m\u001b[43mequation\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moperands\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore[attr-defined]\u001b[39;00m\n\u001b[1;32m 387\u001b[0m path \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 388\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m opt_einsum\u001b[38;5;241m.\u001b[39mis_available():\n",
"\u001b[0;31mRuntimeError\u001b[0m: einsum(): subscript i has size 2 for operand 1 which does not broadcast with previously seen size 4"
]
}
],
"source": [
"torch.einsum(\"ijb,ikb->ijb\", pyq.CNOT(control=0, target=1).tensor(), pyq.Z(0, diagonal=True).tensor().unsqueeze(1))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "8e03808f-eff3-4e62-aedd-a43920f06e3e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[[ 2.+0.j],\n",
" [ 1.+0.j],\n",
" [ 1.+0.j],\n",
" [ 1.+0.j]],\n",
"\n",
" [[ 1.+0.j],\n",
" [ 2.+0.j],\n",
" [ 1.+0.j],\n",
" [ 1.+0.j]],\n",
"\n",
" [[-1.+0.j],\n",
" [-1.+0.j],\n",
" [-1.+0.j],\n",
" [ 0.+0.j]],\n",
"\n",
" [[-1.+0.j],\n",
" [-1.+0.j],\n",
" [ 0.+0.j],\n",
" [-1.+0.j]]], dtype=torch.complex128)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"czop_seq = pyq.Add([pyq.CNOT(control=0, target=1), pyq.Z(0, diagonal=True), ])\n",
"czop_seq.tensor()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23bfb221-43bd-4332-af56-7775ba370620",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e91ea5f6-d12e-410b-89dc-d69336aa5539",
"metadata": {},
"outputs": [],
"source": [
"# czop_diag_example.operation, czop_diag_example.identity"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39537b31-5a18-421f-bc43-d41e635c9a46",
"metadata": {},
"outputs": [],
"source": [
"# parametric_unitary(torch.tensor([1.0]), czop_diag_example.operation, czop_diag_example.identity, 1, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3898b9f-9679-4458-8c5b-8b33f19071ba",
"metadata": {},
"outputs": [],
"source": [
"val_param = {\"theta\": torch.Tensor([1.0])}\n",
"projector_apply_res = czop_example(state_111, val_param)\n",
"projector_apply_res_diag = czop_diag_example(state_111, val_param)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3246ef15-e3a8-43fa-bdcb-f07f1cc1b242",
"metadata": {},
"outputs": [],
"source": [
"torch.allclose(-state_111, projector_apply_res, atol=1.0e-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0770d956-d6fb-4ca5-9245-69c79ece084b",
"metadata": {},
"outputs": [],
"source": [
"torch.allclose(-state_111, projector_apply_res_diag, atol=1.0e-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d364a4e-189c-4d3d-9eb0-840065251b00",
"metadata": {},
"outputs": [],
"source": [
"torch.diag(czop_example.tensor()[...,0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd074ba2-c8fc-4e00-b402-9a26a5f6e34e",
"metadata": {},
"outputs": [],
"source": [
"torch.diag(czop_diag_example.tensor()[...,0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ac5790d-4142-40cc-999c-e1a351bf354c",
"metadata": {},
"outputs": [],
"source": [
"from pyqtorch.matrices import ZDIAG, controlled"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7369df6-3c40-481f-acc0-b18802e79704",
"metadata": {},
"outputs": [],
"source": [
"controlled(ZDIAG.unsqueeze(-1), batch_size=1, n_control_qubits=2, diagonal=True).squeeze(-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d681058-2861-4e46-8afa-6ffaa507bc94",
"metadata": {},
"outputs": [],
"source": [
"ZDIAG.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c70de5a5-c2db-4c0a-8689-91a58fe38452",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b3c21b0-6d03-4de1-a29f-52943c9d99e8",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading