{ "cells": [ { "cell_type": "markdown", "id": "89c0b5de", "metadata": {}, "source": [ "## Demo Play" ] }, { "cell_type": "markdown", "id": "5a7b464d", "metadata": {}, "source": [ "This section outlines the procedure for executing the Block-Encoding method using `PItBE`.\n", "It is recommended to read through this before using this module." ] }, { "cell_type": "markdown", "id": "a4430864", "metadata": {}, "source": [ "### **Computation Conditions**\n", "\n", "- Target matrix to reproduce on the quantum circuit\n", "Hamiltonian of the transverse-field Ising model for a 6-particle system:​\t\n", "\n", "$$\n", "\\hat{H} = 0.5\\sum_i^{6}\\hat{Z}_i\\hat{Z}_{i+1} + 0.8\\sum_i^{6}\\hat{X}_i\n", "$$\n", " \n", "- Quantum state to which the matrix is applied\n", "The system where all spins are aligned in the $+z$ -direction:\n", "$$\n", " |\\psi\\rangle = |000000\\rangle\n", " $$\n" ] }, { "cell_type": "markdown", "id": "087799c6", "metadata": {}, "source": [ "### **Execution**\n", "\n", "#### **Preparation for Execution**\n", "\n", "First, construct the unitary matrix that determines the quantum state of the ancillary qubits.\\\n", "Ideally, this would be represented as a product of Pauli rotation gates; however, there is currently no proposed method to create a product of Pauli rotation gates that generates an arbitrary quantum state.\\\n", "Therefore, in this work, we use a unitary gate that acts on the ancillary qubits initially in the all-zero state and transforms them into an arbitrary quantum state." ] }, { "cell_type": "code", "execution_count": null, "id": "63f67afd", "metadata": {}, "outputs": [], "source": [ "import math\n", "import numpy as np \n", "import pitbe\n", "\n", "# The coefficients and Pauli matrix production \n", "# in the linear combination representation of the matrix to be Block-Encoded\n", "coefficients = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8]\n", "paulies = ['Z0Z1', 'Z1Z2', 'Z2Z3', 'Z3Z4', 'Z4Z5', 'Z5Z0', \n", " 'X0', 'X1', 'X2', 'X3', 'X4', 'X5']\n", "\n", "# Normalize coefficients\n", "norm_cf = np.sum(np.abs(coefficients))\n", "alphas = (np.sqrt(np.abs(coefficients)) / np.sqrt(np.sum(np.abs(coefficients))))\n", "if math.floor(np.log2(len(alphas))) != np.log2(len(alphas)):\n", " zero_list = np.zeros(2**(math.floor(np.log2(len(alphas))) + 1) - len(alphas))\n", " alphas = np.append(alphas, zero_list)\n", "\n", "# Creating a matrix for adjusting coefficient signs\n", "opposite_list = np.ones(len(alphas))\n", "for j in range(len(coefficients)):\n", " if (coefficients[j] < 0):\n", " opposite_list[j] = -1\n", "\n", "# Creating a matrix for adjusting ancilla qubits\n", "cf = pitbe.coeff_make(alphas)\n", "mat_for_anci = pitbe.mat_maker(alphas, cf)\n" ] }, { "cell_type": "markdown", "id": "da75f170", "metadata": {}, "source": [ "#### **Construct Quantum Circuit**\n", "\n", "Next, construct the quantum circuit that performs the BE method\\.\n", "For details on the circuit structure and flow, please refer to the page explaining the BE method.\n", "In this example, we use `Qulacs` as the quantum simulator." ] }, { "cell_type": "code", "execution_count": null, "id": "e3d74848", "metadata": {}, "outputs": [], "source": [ "from qulacs import QuantumState, QuantumCircuit\n", "from qulacs.state import inner_product\n", "from qulacs.gate import X, Y, Z, DenseMatrix, H, CNOT, to_matrix_gate, CZ, RY, RZ, merge\n", "from qulacs.observable import create_observable_from_openfermion_text\n", "from qulacs.quantum_operator import create_quantum_operator_from_openfermion_file\n", "from qulacs.quantum_operator import create_quantum_operator_from_openfermion_text\n", "from qulacsvis import circuit_drawer\n", "\n", "# Detect the number of main qubits and ancilla qubits\n", "main = pitbe.total_search(paulies)\n", "anci = int(np.log2(len(alphas)))\n", "\n", "# Prepare the control qubit information\n", "cont_list = []\n", "for j in range(len(paulies)):\n", " cont_list.append(pitbe.cont_order(j, anci))\n", "\n", "# Create quantum circuit and quantum state\n", "total = anci + main\n", "state = QuantumState(total)\n", "state.set_zero_state()\n", "circ = QuantumCircuit(total)\n", "\n", "# Convert matrices into quantum gates\n", "gate = DenseMatrix([j for j in range(anci)], mat_for_anci)\n", "opp_gate = DenseMatrix([j for j in range(anci)], np.diag(opposite_list))\n", "gate_dag = gate.get_inverse()\n", "\n", "# Create quantum circuit\n", "circ.add_gate(gate)\n", "circ.add_gate(opp_gate) \n", "for j in range(len(cont_list)):\n", " pitbe.circ_make(paulies[j], cont_list[j], circ, total, anci)\n", "circ.add_gate(gate_dag)\n" ] }, { "cell_type": "markdown", "id": "bb66b315", "metadata": {}, "source": [ "#### **Execution of the Quantum Circuit and Analysis of the Results**\n", "\n", "Finally, execute the quantum circuit and analyze the results.\\\n", "Specifically, extract the outcomes where all ancillary qubits are measured as `0`, adjust the corresponding coefficients, and output the processed result.\\\n", "As noted on the page explaining the BE method, the output of this process provides information about the quantum state after applying the reconstructed matrix—not its eigenvalues directly." ] }, { "cell_type": "code", "execution_count": null, "id": "f19c66f7", "metadata": {}, "outputs": [], "source": [ "# Run the quantum circuit\n", "circ.update_quantum_state(state)\n", "\n", "# Calculate probability to get the result of Block-Encoding\n", "obser_order = []\n", "for j in range(anci):\n", " obser_order.append(0)\n", "for j in range(main):\n", " obser_order.append(2) \n", "prob = state.get_marginal_probability(obser_order)\n", "\n", "# Analyze the result of Block-Encoding\n", "res_state = state.get_vector()\n", "desire_state = []\n", "for i in range(2**anci):\n", " desire_state.append(res_state[2**anci*i]*norm_cf)\n", "print(desire_state)" ] }, { "cell_type": "markdown", "id": "08e2c020", "metadata": {}, "source": [ "This result matches the outcome of directly applying the Hamiltonian $\\hat{H}$ to the quantum state $|\\psi\\rangle$, $\\hat{H}|\\psi\\rangle$.\\\n", "The above describes the complete workflow for executing the BE method using the `PItBE` module." ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }