{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# qiskit_circ_make" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 概要" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "この関数は与えられた情報からQiskitで実行可能なBlock-Encodingを実行する回路を自動的に組み上げる" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 引数一覧" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "|argument name|type|role|\n", "|---|---|---|\n", "|gate_inf|str|パウリ行列積の情報|\n", "|zero_one|list(elements:int)|制御ビットの状態に関する情報|\n", "|circ|QuantumCircuit(qulacs)|組み上げ先の回路|\n", "|qubit|int|量子回路に必要な総ビット数|\n", "|ancilla|int|量子回路に必要な補助ビット数|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "def qiskit_circ_make(gate_inf, zero_one, circ, qubit, ancilla):\n", " \"\"\"\n", " This function automatically constructs a quantum circuit for Qiskit that performs block encoding based on the given information.\n", "\n", " Parameters:\n", " gate_inf: the information of Pauli matrix product\n", " zero_one: the information of control qubits\n", " circ: a pre-defined quantum circuit used for actual computation\n", " qubit: the number of qubits which express the quantum state\n", " ancilla: the number of ancilla qubits for Block-Encoding \n", "\n", " Returns:\n", " None. \n", " A quantum circuit implementing Pauli operators with control qubits is created on circ.\n", " \"\"\"\n", " # Local Values\n", " work_ope_order = [] # A list to store information about Pauli matrix product\n", " input_switch = 0 # A switch to initiate reading information about Pauli matrix product\n", " input_ele = \"\" # A temporary list to store information about Pauli matrix product\n", " # If Pauli matrix product is Identity,\n", " if gate_inf == \"\":\n", " gate_a = [[1., 0.],\n", " [0., 1.]]\n", " unitary_gate = Operator(np.array(gate_a)).to_instruction()\n", " for i in range(int(qubit - ancilla)):\n", " circ.append(unitary_gate, [i])\n", " # If Pauli matrix product is the product of some Pauli matirces (ex: X0Y2Z4Y5)\n", " else:\n", " # Read the Pauli operators that make up the Pauli matrix product\n", " for i in range(len(gate_inf)):\n", " # Determine whether to interpret each item as a coefficient, a Pauli matrix product, or to ignore it.\n", " # 0: Read, 1: ignore\n", " if i < len(gate_inf) - 1.5:\n", " if input_switch > 0.3:\n", " work_ope_order.append(input_ele)\n", " input_ele = \"\"\n", " input_switch = 0\n", " if gate_inf[i+1] == \"X\":\n", " input_switch = 1\n", " if gate_inf[i+1] == \"Y\":\n", " input_switch = 1\n", " if gate_inf[i+1] == \"Z\":\n", " input_switch = 1\n", " if gate_inf[i+1] == \"I\":\n", " input_switch = 1\n", " input_ele += gate_inf[i]\n", " else:\n", " work_ope_order.append(input_ele)\n", " input_switch = 0\n", " work_ope_order[-1] += gate_inf[-1]\n", " # Construct a quantum circuit from the reading results\n", " for i in range(len(work_ope_order)):\n", " num_inf = \"\"\n", " for j in range(len(work_ope_order[i])-1):\n", " num_inf += work_ope_order[i][j+1]\n", " tag_num = int(num_inf)\n", " gate_pos = qubit - tag_num - 1\n", " gate_pos = ancilla + tag_num\n", " cont_list = []\n", " for j in range(ancilla):\n", " cont_list.append(j)\n", " cont_list.append(gate_pos)\n", " if work_ope_order[i][0] == \"X\":\n", " add_gate = XGate().control(ancilla, ctrl_state=zero_one)\n", " circ.append(add_gate, cont_list)\n", " elif work_ope_order[i][0] == \"Y\":\n", " add_gate = YGate().control(ancilla, ctrl_state=zero_one)\n", " circ.append(add_gate, cont_list)\n", " elif work_ope_order[i][0] == \"Z\":\n", " add_gate = ZGate().control(ancilla, ctrl_state=zero_one)\n", " circ.append(add_gate, cont_list)\n", " elif work_ope_order[i][0] == \"I\":\n", " add_gate = IGate().control(ancilla, ctrl_state=zero_one)\n", " circ.append(add_gate, cont_list)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 実行例" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pitbe\n", "import qiskit\n", "#from qiskit import QuantumCircuit\n", "from qiskit.circuit.library import ZGate, XGate, YGate, IGate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circuit = qiskit.QuantumCircuit(4)\n", "ope_lst = [\"I0I1\", \"X0X1\", \"Y0Y1\", \"Z0I1\"]\n", "cont_list = [\"00\", \"01\", \"10\", \"11\"]\n", "\n", "for j in range(len(cont_list)):\n", " pitbe.qiskit_circ_make(ope_lst[j], cont_list[j], circuit, 4, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 作成された回路図" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Quantum Circuit](./pennydemo2.jpeg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 注意点" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "この関数は「Qiskit」向けであるため、\n", "必ず関数のimport部分で「qiskit」をimportすること\\\n", "関数circ_makeとは用いる量子計算機シミュレータが異なるため間違えないように注意すること" ] } ], "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.11.6" } }, "nbformat": 4, "nbformat_minor": 2 }