{ "cells": [ { "cell_type": "markdown", "id": "8ee27bcb", "metadata": {}, "source": [ "## qiskit_circ_make" ] }, { "cell_type": "markdown", "id": "a12c2de6", "metadata": {}, "source": [ "### **Outline**\n", "This function automatically constructs a quantum circuit that performs Block-Encoding using `Qiskit`, based on the provided input." ] }, { "cell_type": "markdown", "id": "c0c86879", "metadata": {}, "source": [ "### **Index List**\n", "\n", "|argument name|type|role|\n", "|---|---|---|\n", "|gate_inf|str|Information about the production of Pauli matrices|\n", "|zero_one|list(elements:int)|Information about the state of control qubits|\n", "|circ|QuantumCircuit(qulacs)|Gate-targeting quantum circuit|\n", "|qubit|int|the number of qubits|\n", "|ancilla|int|the number of ancilla qubits|" ] }, { "cell_type": "markdown", "id": "8340a2ce", "metadata": {}, "source": [ "### **Python code**\n", "```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", "id": "515ac3df", "metadata": {}, "source": [ "### **Sample Run**" ] }, { "cell_type": "code", "execution_count": null, "id": "a8535883", "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, "id": "d47ad46f", "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", "id": "ca931175", "metadata": {}, "source": [ "### **Constructed Quantum Circuit**" ] }, { "cell_type": "markdown", "id": "3124d210", "metadata": {}, "source": [ "![Quantum Circuit](../../picture/pennydemo2.jpeg)" ] }, { "cell_type": "markdown", "id": "3fe6f920", "metadata": {}, "source": [ "### **Caution**\n", "\n", "This function is intended for use with `Qiskit` and may not execute correctly with other platforms.\\\n", "For `Qulacs`, please use [circ_make](../eng/circ_make_en.ipynb) provided for that framework." ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }