{ "cells": [ { "cell_type": "markdown", "id": "80e1730a", "metadata": {}, "source": [ "## circ_make" ] }, { "cell_type": "markdown", "id": "63622d20", "metadata": {}, "source": [ "### **Outline**\n", "This function automatically constructs a quantum circuit that performs Block-Encoding using `Qulacs`, based on the provided input." ] }, { "cell_type": "markdown", "id": "159b3caa", "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": "b39fb965", "metadata": {}, "source": [ "### **Python code**\n", "```python\n", "def circ_make(gate_inf, zero_one, circ, qubit, ancilla):\n", " \"\"\"\n", " This function automatically constructs a quantum circuit for Qulacs 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", " for i in range(int(qubit - ancilla)):\n", " gate = DenseMatrix(int(ancilla + i), gate_a)\n", " mat_no = to_matrix_gate(gate)\n", " for j in range(len(zero_one)):\n", " cont_pos = len(zero_one) - j - 1\n", " mat_no.add_control_qubit(cont_pos, zero_one[j])\n", " circ.add_gate(mat_no)\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", " if work_ope_order[i][0] == \"X\":\n", " gate_a = X(gate_pos)\n", " mat_no = to_matrix_gate(gate_a)\n", " for j in range(len(zero_one)):\n", " cont_pos = len(zero_one) - j - 1\n", " mat_no.add_control_qubit(cont_pos, \n", " zero_one[j])\n", " elif work_ope_order[i][0] == \"Y\":\n", " gate_a = Y(gate_pos)\n", " mat_no = to_matrix_gate(gate_a)\n", " for j in range(len(zero_one)):\n", " cont_pos = len(zero_one) - j - 1\n", " mat_no.add_control_qubit(cont_pos, \n", " zero_one[j])\n", " elif work_ope_order[i][0] == \"Z\":\n", " gate_a = Z(gate_pos)\n", " mat_no = to_matrix_gate(gate_a)\n", " for j in range(len(zero_one)):\n", " cont_pos = len(zero_one) - j - 1\n", " mat_no.add_control_qubit(cont_pos, \n", " zero_one[j])\n", " elif work_ope_order[i][0] == \"I\":\n", " gate_a = [[1, 0],\n", " [0, 1]]\n", " gate = DenseMatrix(int(gate_inf[2*i+1]), gate_a)\n", " mat_no = to_matrix_gate(gate)\n", " for j in range(len(zero_one)):\n", " cont_pos = len(zero_one) - j - 1\n", " mat_no.add_control_qubit(cont_pos, \n", " zero_one[j])\n", " circ.add_gate(mat_no)\n", "```" ] }, { "cell_type": "markdown", "id": "09fd4009", "metadata": {}, "source": [ "### **Sample Run**" ] }, { "cell_type": "code", "execution_count": null, "id": "4d1dda56", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pitbe\n", "from qulacs import QuantumState, QuantumCircuit\n", "from qulacs.gate import X, Y, Z, DenseMatrix, to_matrix_gate" ] }, { "cell_type": "code", "execution_count": null, "id": "a224a496", "metadata": {}, "outputs": [], "source": [ "circuit = QuantumCircuit(4)\n", "ope_lst = [\"I0I1\", \"X0X1\", \"Y0Y1\", \"Z0I1\"]\n", "cont_list = [[0, 0], [1, 0],\n", " [0, 1], [1, 1]]\n", "\n", "for j in range(len(cont_list)):\n", " pitbe.circ_make(ope_lst[j], cont_list[j], circuit, 4, 2)" ] }, { "cell_type": "markdown", "id": "b97be206", "metadata": {}, "source": [ "### **Constructed Quantum Circuit**" ] }, { "cell_type": "markdown", "id": "eba8014d", "metadata": {}, "source": [ "![Quantum Circuit](../../picture/pennydemo2.jpeg)" ] }, { "cell_type": "markdown", "id": "2ed8230d", "metadata": {}, "source": [ "### **Caution**\n", "\n", "This function is intended for use with `Qulacs` and may not execute correctly with other platforms.\\\n", "For `Qiskit`, please use [qiskit_circ_make](../eng/qiskit_circ_make_en.ipynb) provided for that framework." ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }