Loading examples/sycamore/CMakeLists.txt +7 −1 Original line number Diff line number Diff line Loading @@ -24,3 +24,9 @@ target_compile_definitions(sycamore_circ_amplitude_compare PRIVATE RESOURCE_DIR= add_executable(sycamore_circ_amplitude_conjugate_layer sycamore_circ_amplitude_conjugate_layer.cpp) target_link_libraries(sycamore_circ_amplitude_conjugate_layer PRIVATE xacc::xacc tnqvm) target_compile_definitions(sycamore_circ_amplitude_conjugate_layer PRIVATE RESOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources") add_executable(sycamore_circ_slice_samples sycamore_circ_slice_samples.cpp) target_link_libraries(sycamore_circ_slice_samples PRIVATE xacc::xacc tnqvm) target_compile_definitions(sycamore_circ_slice_samples PRIVATE RESOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources") examples/sycamore/sycamore_amplitude.py 0 → 100644 +37 −0 Original line number Diff line number Diff line import xacc import os from pathlib import Path import math dir_path = os.path.dirname(os.path.realpath(__file__)) RESOURCE_DIR = dir_path + "/resources" CIRCUIT_DEPTH = 1 XASM_SRC_FILE = RESOURCE_DIR + "/sycamore_53_" + str(CIRCUIT_DEPTH) + "_0.xasm" xasmSrcStr = Path(XASM_SRC_FILE).read_text() xasmCompiler = xacc.getCompiler("xasm") ir = xasmCompiler.compile(xasmSrcStr) program = ir.getComposites()[0] BIT_STRING = [] N_QUBITS = 53 INITIAL_STATE_BIT_STRING = [] for i in range(N_QUBITS): BIT_STRING.append(0) INITIAL_STATE_BIT_STRING.append(0) qpu = xacc.getAccelerator("tnqvm", { "tnqvm-visitor": "exatn", "bitstring": BIT_STRING, "exatn-buffer-size-gb": 2 }) qubitReg = xacc.qalloc(N_QUBITS) qpu.execute(qubitReg, program) #print(qubitReg) realAmpl = qubitReg["amplitude-real"] imagAmpl = qubitReg["amplitude-imag"] print("================= RESULT =================") print(INITIAL_STATE_BIT_STRING, " -->", BIT_STRING, ":", realAmpl, imagAmpl) print("Bit-string probability:", math.sqrt(realAmpl*realAmpl + imagAmpl*imagAmpl)) No newline at end of file examples/sycamore/sycamore_circ_amplitude.cpp +5 −4 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ int main(int argc, char **argv) xacc::Initialize(); //xacc::set_verbose(true); //xacc::logToFile(true); //xacc::setLoggingLevel(2); //xacc::setLoggingLevel(1); // Options: 4, 5, 6, 8, 10, 12, 14, 16, 18, 20 const int CIRCUIT_DEPTH = 8; Loading @@ -46,9 +46,10 @@ int main(int argc, char **argv) // (1) "exatn" == "exatn:double" uses double (64-bit) type. // (1) "exatn:float" uses float (32-bit) type. auto qpu = xacc::getAccelerator("tnqvm", { std::make_pair("tnqvm-visitor", "exatn"), std::make_pair("tnqvm-visitor", "exatn:float"), std::make_pair("bitstring", BIT_STRING), std::make_pair("exatn-buffer-size-gb", 2) //std::make_pair("exatn-contract-seq-optimizer", "cotengra") }); // Allocate a register of 53 qubits Loading examples/sycamore/sycamore_circ_slice_samples.cpp 0 → 100644 +77 −0 Original line number Diff line number Diff line // Computes a single amplitude from a Sycamore circuit via full tensor contraction #include "xacc.hpp" #include <iostream> #include <fstream> #include <numeric> #include <cassert> // Initial state: const std::vector<int> INITIAL_STATE_BIT_STRING(53, 0); std::string bitStringVecToString(const std::vector<int>& in_vec) { std::stringstream s; for (const auto& bit: in_vec) s << bit; return s.str(); } int main(int argc, char **argv) { xacc::Initialize(); xacc::set_verbose(true); xacc::logToFile(true); xacc::setLoggingLevel(1); // Options: 4, 5, 6, 8, 10, 12, 14, 16, 18, 20 const int CIRCUIT_DEPTH = 4; // Construct the full path to the XASM source file const std::string XASM_SRC_FILE = std::string(RESOURCE_DIR) + "/sycamore_53_" + std::to_string(CIRCUIT_DEPTH) + "_0.xasm"; // Read XASM source std::ifstream inFile; inFile.open(XASM_SRC_FILE); std::stringstream strStream; strStream << inFile.rdbuf(); const std::string kernelName = "sycamoreCirc"; std::string xasmSrcStr = strStream.str(); // Construct a unique kernel name: const std::string newKernelName = kernelName + "_" + std::to_string(CIRCUIT_DEPTH); xasmSrcStr.replace(xasmSrcStr.find(kernelName), kernelName.length(), newKernelName); const int NB_OPEN_QUBITS = 21; // The bitstring to calculate amplitude // Example: bitstring = 000000000...00 (-1 -1) // there are NB_OPEN_QUBITS open legs at the end (-1) values std::vector<int> BIT_STRING(53, 0); std::fill_n(BIT_STRING.begin() + (BIT_STRING.size() - NB_OPEN_QUBITS), NB_OPEN_QUBITS, -1); // ExaTN visitor: // Note: // (1) "exatn" == "exatn:double" uses double (64-bit) type. // (1) "exatn:float" uses float (32-bit) type. const std::string OPTIMIZER_NAME = "cotengra"; // "cotengra" auto qpu = xacc::getAccelerator("tnqvm", { std::make_pair("tnqvm-visitor", "exatn"), std::make_pair("bitstring", BIT_STRING), std::make_pair("exatn-buffer-size-gb", 2), std::make_pair("exatn-contract-seq-optimizer", OPTIMIZER_NAME) }); // Allocate a register of 53 qubits auto qubitReg = xacc::qalloc(53); // Compile the XASM program auto xasmCompiler = xacc::getCompiler("xasm"); auto ir = xasmCompiler->compile(xasmSrcStr, qpu); auto program = ir->getComposites()[0]; qpu->execute(qubitReg, program); // qubitReg->print(); const auto realAmpl = (*qubitReg)["amplitude-real-vec"].as<std::vector<double>>(); const auto imagAmpl = (*qubitReg)["amplitude-imag-vec"].as<std::vector<double>>(); // Open 21 legs assert(realAmpl.size() == (1ULL << NB_OPEN_QUBITS)); assert(imagAmpl.size() == (1ULL << NB_OPEN_QUBITS)); std::cout << "Slice vector of size: " << realAmpl.size() << "\n"; xacc::Finalize(); return 0; } tnqvm/visitors/exatn-gen/ExatnGenVisitor.hpp +3 −3 Original line number Diff line number Diff line Loading @@ -31,13 +31,13 @@ // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | Initialization Parameter | Parameter Description | type | default | // +=============================+========================================================================+=============+==========================+ // | reconstruct-layers | Perform reconstruction after this number of 2-q gates. | int | -1 (no reconstruct | // | reconstruct-layers | Perform reconstruction after this number of 2-q gates | int | -1 (no reconstruct) | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | reconstruct-tolerance | Reconstruction convergence tolerance | double | 1e-4 | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | max-bond-dim | Reconstruction max MPS bond dimension | int | 512 | // | max-bond-dim | Reconstruction max bond dimension | int | 512 | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | reconstruct-builder | Reconstruction network builder | string | "MPS" | // | reconstruct-builder | Reconstruction network builder (tensor network ansatz) | string | "MPS" | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ #pragma once Loading Loading
examples/sycamore/CMakeLists.txt +7 −1 Original line number Diff line number Diff line Loading @@ -24,3 +24,9 @@ target_compile_definitions(sycamore_circ_amplitude_compare PRIVATE RESOURCE_DIR= add_executable(sycamore_circ_amplitude_conjugate_layer sycamore_circ_amplitude_conjugate_layer.cpp) target_link_libraries(sycamore_circ_amplitude_conjugate_layer PRIVATE xacc::xacc tnqvm) target_compile_definitions(sycamore_circ_amplitude_conjugate_layer PRIVATE RESOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources") add_executable(sycamore_circ_slice_samples sycamore_circ_slice_samples.cpp) target_link_libraries(sycamore_circ_slice_samples PRIVATE xacc::xacc tnqvm) target_compile_definitions(sycamore_circ_slice_samples PRIVATE RESOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources")
examples/sycamore/sycamore_amplitude.py 0 → 100644 +37 −0 Original line number Diff line number Diff line import xacc import os from pathlib import Path import math dir_path = os.path.dirname(os.path.realpath(__file__)) RESOURCE_DIR = dir_path + "/resources" CIRCUIT_DEPTH = 1 XASM_SRC_FILE = RESOURCE_DIR + "/sycamore_53_" + str(CIRCUIT_DEPTH) + "_0.xasm" xasmSrcStr = Path(XASM_SRC_FILE).read_text() xasmCompiler = xacc.getCompiler("xasm") ir = xasmCompiler.compile(xasmSrcStr) program = ir.getComposites()[0] BIT_STRING = [] N_QUBITS = 53 INITIAL_STATE_BIT_STRING = [] for i in range(N_QUBITS): BIT_STRING.append(0) INITIAL_STATE_BIT_STRING.append(0) qpu = xacc.getAccelerator("tnqvm", { "tnqvm-visitor": "exatn", "bitstring": BIT_STRING, "exatn-buffer-size-gb": 2 }) qubitReg = xacc.qalloc(N_QUBITS) qpu.execute(qubitReg, program) #print(qubitReg) realAmpl = qubitReg["amplitude-real"] imagAmpl = qubitReg["amplitude-imag"] print("================= RESULT =================") print(INITIAL_STATE_BIT_STRING, " -->", BIT_STRING, ":", realAmpl, imagAmpl) print("Bit-string probability:", math.sqrt(realAmpl*realAmpl + imagAmpl*imagAmpl)) No newline at end of file
examples/sycamore/sycamore_circ_amplitude.cpp +5 −4 Original line number Diff line number Diff line Loading @@ -19,7 +19,7 @@ int main(int argc, char **argv) xacc::Initialize(); //xacc::set_verbose(true); //xacc::logToFile(true); //xacc::setLoggingLevel(2); //xacc::setLoggingLevel(1); // Options: 4, 5, 6, 8, 10, 12, 14, 16, 18, 20 const int CIRCUIT_DEPTH = 8; Loading @@ -46,9 +46,10 @@ int main(int argc, char **argv) // (1) "exatn" == "exatn:double" uses double (64-bit) type. // (1) "exatn:float" uses float (32-bit) type. auto qpu = xacc::getAccelerator("tnqvm", { std::make_pair("tnqvm-visitor", "exatn"), std::make_pair("tnqvm-visitor", "exatn:float"), std::make_pair("bitstring", BIT_STRING), std::make_pair("exatn-buffer-size-gb", 2) //std::make_pair("exatn-contract-seq-optimizer", "cotengra") }); // Allocate a register of 53 qubits Loading
examples/sycamore/sycamore_circ_slice_samples.cpp 0 → 100644 +77 −0 Original line number Diff line number Diff line // Computes a single amplitude from a Sycamore circuit via full tensor contraction #include "xacc.hpp" #include <iostream> #include <fstream> #include <numeric> #include <cassert> // Initial state: const std::vector<int> INITIAL_STATE_BIT_STRING(53, 0); std::string bitStringVecToString(const std::vector<int>& in_vec) { std::stringstream s; for (const auto& bit: in_vec) s << bit; return s.str(); } int main(int argc, char **argv) { xacc::Initialize(); xacc::set_verbose(true); xacc::logToFile(true); xacc::setLoggingLevel(1); // Options: 4, 5, 6, 8, 10, 12, 14, 16, 18, 20 const int CIRCUIT_DEPTH = 4; // Construct the full path to the XASM source file const std::string XASM_SRC_FILE = std::string(RESOURCE_DIR) + "/sycamore_53_" + std::to_string(CIRCUIT_DEPTH) + "_0.xasm"; // Read XASM source std::ifstream inFile; inFile.open(XASM_SRC_FILE); std::stringstream strStream; strStream << inFile.rdbuf(); const std::string kernelName = "sycamoreCirc"; std::string xasmSrcStr = strStream.str(); // Construct a unique kernel name: const std::string newKernelName = kernelName + "_" + std::to_string(CIRCUIT_DEPTH); xasmSrcStr.replace(xasmSrcStr.find(kernelName), kernelName.length(), newKernelName); const int NB_OPEN_QUBITS = 21; // The bitstring to calculate amplitude // Example: bitstring = 000000000...00 (-1 -1) // there are NB_OPEN_QUBITS open legs at the end (-1) values std::vector<int> BIT_STRING(53, 0); std::fill_n(BIT_STRING.begin() + (BIT_STRING.size() - NB_OPEN_QUBITS), NB_OPEN_QUBITS, -1); // ExaTN visitor: // Note: // (1) "exatn" == "exatn:double" uses double (64-bit) type. // (1) "exatn:float" uses float (32-bit) type. const std::string OPTIMIZER_NAME = "cotengra"; // "cotengra" auto qpu = xacc::getAccelerator("tnqvm", { std::make_pair("tnqvm-visitor", "exatn"), std::make_pair("bitstring", BIT_STRING), std::make_pair("exatn-buffer-size-gb", 2), std::make_pair("exatn-contract-seq-optimizer", OPTIMIZER_NAME) }); // Allocate a register of 53 qubits auto qubitReg = xacc::qalloc(53); // Compile the XASM program auto xasmCompiler = xacc::getCompiler("xasm"); auto ir = xasmCompiler->compile(xasmSrcStr, qpu); auto program = ir->getComposites()[0]; qpu->execute(qubitReg, program); // qubitReg->print(); const auto realAmpl = (*qubitReg)["amplitude-real-vec"].as<std::vector<double>>(); const auto imagAmpl = (*qubitReg)["amplitude-imag-vec"].as<std::vector<double>>(); // Open 21 legs assert(realAmpl.size() == (1ULL << NB_OPEN_QUBITS)); assert(imagAmpl.size() == (1ULL << NB_OPEN_QUBITS)); std::cout << "Slice vector of size: " << realAmpl.size() << "\n"; xacc::Finalize(); return 0; }
tnqvm/visitors/exatn-gen/ExatnGenVisitor.hpp +3 −3 Original line number Diff line number Diff line Loading @@ -31,13 +31,13 @@ // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | Initialization Parameter | Parameter Description | type | default | // +=============================+========================================================================+=============+==========================+ // | reconstruct-layers | Perform reconstruction after this number of 2-q gates. | int | -1 (no reconstruct | // | reconstruct-layers | Perform reconstruction after this number of 2-q gates | int | -1 (no reconstruct) | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | reconstruct-tolerance | Reconstruction convergence tolerance | double | 1e-4 | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | max-bond-dim | Reconstruction max MPS bond dimension | int | 512 | // | max-bond-dim | Reconstruction max bond dimension | int | 512 | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ // | reconstruct-builder | Reconstruction network builder | string | "MPS" | // | reconstruct-builder | Reconstruction network builder (tensor network ansatz) | string | "MPS" | // +-----------------------------+------------------------------------------------------------------------+-------------+--------------------------+ #pragma once Loading