Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement qir-qsim app for dynamic measurement handling #17

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1519286
Implement qir-qsim app for dynamic measurement handling
wongey Nov 6, 2024
177aaa4
Remove redudant qsim folder
wongey Nov 6, 2024
39223c3
Clean up results printing and output from Vicente
wongey Nov 6, 2024
c699ae4
Add dyanamic BV example from Vicente
wongey Nov 6, 2024
65054f2
Revert changes to upstream and remove tpls
sethrj Nov 22, 2024
63874bc
Update goldfinger configuration
sethrj Nov 22, 2024
91753d6
Add qsim
sethrj Nov 22, 2024
e289381
Update version to get include directories working
sethrj Nov 22, 2024
ac7c244
Run clang format
sethrj Nov 22, 2024
ece521f
Add qsim dynamicbv test
wongey Nov 25, 2024
713ace6
Merge branch 'ORNL-QCI:main' into qir-qsim
wongey Nov 25, 2024
a34f190
Capitalize class names q -> Q
wongey Nov 26, 2024
8fb40f3
Update qsim file names
wongey Nov 26, 2024
25d9433
Add examples
wongey Nov 26, 2024
22d0ac9
Minor formatting
wongey Nov 26, 2024
29be7ae
Resolve seed issue
wongey Nov 27, 2024
19867f3
Minor formatting
wongey Nov 27, 2024
ede6e4a
Update qsim unit test
wongey Nov 27, 2024
6fe3474
Refactor to move public to private types
wongey Nov 27, 2024
24a06bb
Unused variables and naming
sethrj Nov 27, 2024
aadbb8a
Use PIMPL
sethrj Nov 27, 2024
6f13b04
Delete tuple runtime, fix errors
sethrj Nov 27, 2024
35778ad
Fix qsim test
wongey Nov 27, 2024
8520063
Merge branch 'main' into qir-qsim
wongey Dec 13, 2024
4658b44
Merge branch 'main' into qir-qsim
wongey Dec 18, 2024
e77166c
Merge branch 'main' into qir-qsim
wongey Dec 18, 2024
6828673
Merge branch 'ORNL-QCI:main' into qir-qsim
wongey Dec 20, 2024
bdd35c4
including OutputDistribution into the libs list
vicenley Dec 23, 2024
cb373ba
old BufferManager now in qiree namespace
vicenley Dec 23, 2024
181737d
deleting the old BufferManager
vicenley Dec 23, 2024
a92aacf
updating libs list
vicenley Dec 23, 2024
040b834
updating manager (Buffer)
vicenley Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#----------------------------------------------------------------------------#

cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.18)

# Set QIREE_VERSION using git tags using the following format
set(CGV_TAG_REGEX "v([0-9.]+)(-dev|-rc.[0-9]+)?")
include("${CMAKE_CURRENT_LIST_DIR}/cmake/CgvFindVersion.cmake")
cgv_find_version(QIREE)

project(QIREE VERSION "${QIREE_VERSION}" LANGUAGES CXX)
cmake_policy(VERSION 3.12...3.22)
cmake_policy(VERSION 3.18...3.30)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(FetchContent)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/QIREEUtils.cmake")

macro(qiree_set_default name value)
Expand All @@ -34,9 +35,11 @@ endmacro()

# Components
option(QIREE_BUILD_DOCS "Build QIR-EE documentation" OFF)
option(QIREE_BUILD_TESTS "Build QIR-EE unit tests" OFF)
option(QIREE_BUILD_TESTS "Build QIR-EE unit tests" ON)
option(QIREE_BUILD_EXAMPLES "Build QIR-EE examples" OFF)
option(QIREE_USE_QSIM "Download and build Google qsim backend" OFF)
option(QIREE_USE_XACC "Build XACC interface" ON)

qiree_set_default(BUILD_TESTING ${QIREE_BUILD_TESTS})

# Assertion handling
Expand Down Expand Up @@ -111,8 +114,28 @@ set(QIREE_RUNTIME_OUTPUT_DIRECTORY
enable_language(C) # Needed for LLVM
find_package(LLVM REQUIRED)
if((LLVM_VERSION VERSION_LESS 14)
OR (LLVM_VERSION VERSION_GREATER_EQUAL 19))
message(WARNING "QIR-EE is only tested with LLVM 14-18: found version ${LLVM_VERSION}")
OR (LLVM_VERSION VERSION_GREATER_EQUAL 20))
message(WARNING "QIR-EE is only tested with LLVM 14-19: found version ${LLVM_VERSION}")
endif()

if(QIREE_USE_QSIM)
# Declare and download qsim: it's header-only and the code is in "lib",
# so download it into "external/qsim" directory and include "external"
FetchContent_Declare(
qsim_content
QUIET
GIT_REPOSITORY https://github.com/quantumlib/qsim.git
GIT_TAG 55b4d0e7ea8f085a1709c2c06ff1e28b3aa93357 # 'main' on 22 Nov 2024
SOURCE_SUBDIR "lib" # Don't load top-level cmake file
SOURCE_DIR "external/qsim"
)
FetchContent_MakeAvailable(qsim_content)
qiree_add_library(qiree_qsim INTERFACE)
add_library(QIREE::qsim ALIAS qiree_qsim)
target_include_directories(qiree_qsim SYSTEM INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/external>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/external>"
)
endif()

if(QIREE_USE_XACC)
Expand Down Expand Up @@ -185,7 +208,7 @@ add_subdirectory(app)
#----------------------------------------------------------------------------#

if(QIREE_BUILD_EXAMPLES)
add_subdirectory(examples)
add_subdirectory(examples)
endif()

#----------------------------------------------------------------------------#
Expand Down
92 changes: 60 additions & 32 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
{
"version": 3,
"cmakeMinimumRequired": {"major": 3, "minor": 21, "patch": 0},
"configurePresets": [
{
"name": "default",
"displayName": "Automatic options (debug with tests)",
"description": "Dependencies are enabled based on environment probing",
"binaryDir": "${sourceDir}/build-${presetName}",
"generator": "Ninja",
"cacheVariables": {
"BUILD_SHARED_LIBS": {"type": "BOOL", "value": "ON"},
"CMAKE_BUILD_TYPE": {"type": "STRING", "value": "Debug"},
"CMAKE_INSTALL_PREFIX": "${sourceDir}/install-${presetName}"
}
}
],
"buildPresets": [
{
"name": "default",
"jobs": 0,
"configurePreset": "default"
}
],
"testPresets": [
{
"name": "default",
"configurePreset": "default",
"output": {"outputOnFailure": true},
"execution": {"noTestsAction": "error", "stopOnFailure": false, "jobs": 8}
}
]
}
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "default",
"displayName": "Automatic options (debug with tests)",
"description": "Dependencies are enabled based on environment probing",
"binaryDir": "${sourceDir}/build-${presetName}",
"generator": "Ninja",
"cacheVariables": {
"BUILD_SHARED_LIBS": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Debug"
},
"CMAKE_INSTALL_PREFIX": "${sourceDir}/install-${presetName}"
}
},
{
"name": "default",
"displayName": "Clang 16.0.6 x86_64-pc-linux-gnu",
"description": "Using compilers: C = /usr/bin/clang-16, CXX = /usr/bin/clang++-16",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_C_COMPILER": "/usr/bin/clang-16",
"CMAKE_CXX_COMPILER": "/usr/bin/clang++-16",
"CMAKE_BUILD_TYPE": "Debug"
}
}
],
"buildPresets": [
{
"name": "default",
"jobs": 0,
"configurePreset": "default"
}
],
"testPresets": [
{
"name": "default",
"configurePreset": "default",
"output": {
"outputOnFailure": true
},
"execution": {
"noTestsAction": "error",
"stopOnFailure": false,
"jobs": 8
}
}
]
}
20 changes: 19 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#-----------------------------------------------------------------------------#

include(FetchContent)
FetchContent_Declare(
# Command Line Parser for C++ programs
cli11_proj
QUIET
GIT_REPOSITORY https://github.com/CLIUtils/CLI11.git
Expand All @@ -14,6 +14,24 @@ FetchContent_Declare(

FetchContent_MakeAvailable(cli11_proj)

#-----------------------------------------------------------------------------#
# QSIM FRONT END
#-----------------------------------------------------------------------------#

if(QIREE_USE_QSIM)
qiree_add_executable(qir-qsim
qir-qsim.cc
)
target_link_libraries(qir-qsim
PUBLIC QIREE::qiree QIREE::qirqsim
PRIVATE CLI11::CLI11
)
endif()

#-----------------------------------------------------------------------------#
# XACC FRONT END
#-----------------------------------------------------------------------------#

if(QIREE_USE_XACC)
qiree_add_executable(qir-xacc
qir-xacc.cc
Expand Down
98 changes: 98 additions & 0 deletions app/qir-qsim.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other QIR-EE developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//---------------------------------------------------------------------------//
//! \file qir-xacc/qir-xacc.cc
//---------------------------------------------------------------------------//
#include <cstdlib>
#include <iostream>
#include <string>
#include <string_view>
#include <CLI/CLI.hpp>

#include "qiree_version.h"

#include "qiree/Executor.hh"
#include "qiree/Module.hh"
#include "qiree/QuantumNotImpl.hh"
#include "qirqsim/QsimDefaultRuntime.hh"
#include "qirqsim/QsimQuantum.hh"

using namespace std::string_view_literals;

namespace qiree
{
namespace app
{
void run(std::string const& filename,
int num_shots)
// bool group_tuples = false)
{
// Load the input
Executor execute{Module{filename}};

// Set up qsim
QsimQuantum sim(std::cout, 0);

// Collect the statistics
std::unique_ptr<RuntimeInterface> rt;
rt = std::make_unique<QsimDefaultRuntime>(std::cout, sim);

// Run several time = shots (default 1)
for (int i = 0; i < num_shots; i++)
{
execute(sim, *rt);
}

std::cout << std::endl;
std::cout << "Measurement output:" << std::endl;
std::cout << "-------------------" << std::endl;
std::cout << "Number of shots: " << num_shots << std::endl;
std::cout << "Number of qubits: " << sim.num_qubits() << std::endl;

for(int q_index = 0; q_index < sim.num_qubits(); q_index++){
int value_0 = 0;
int value_1 = 0;
if (auto value = sim.manager.getBufferValue("q"+std::to_string(q_index), "0"); value.has_value()){ value_0 = value.value();}
if (auto value = sim.manager.getBufferValue("q"+std::to_string(q_index), "1"); value.has_value()){ value_1 = value.value();}
std::cout << "q" << q_index << " {0: " << value_0 << "," << " 1: " << value_1 << "}\n";
}
}

//---------------------------------------------------------------------------//
} // namespace app
} // namespace qiree

//---------------------------------------------------------------------------//
/*!
* Execute and run.
*/
int main(int argc, char* argv[])
{
int num_shots{1};
std::string filename;
//bool group_tuples{false};

CLI::App app;

auto* filename_opt
= app.add_option("--input,-i,input", filename, "QIR input file");
filename_opt->required();

auto* nshot_opt
= app.add_option("-s,--shots", num_shots, "Number of shots");
nshot_opt->capture_default_str();

//app.add_flag("--group-tuples,!--no-group-tuples",
// group_tuples,
// "Print per-tuple measurement statistics rather than "
// "per-qubit");

CLI11_PARSE(app, argc, argv);

//qiree::app::run(filename, num_shots, group_tuples);
qiree::app::run(filename, num_shots);

return EXIT_SUCCESS;
}
43 changes: 43 additions & 0 deletions examples/bell_ccx.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; ModuleID = 'Bell_ccx'
source_filename = "Bell_ccx"

%Qubit = type opaque
%Result = type opaque

define void @main() #0 {
entry:
call void @__quantum__qis__h__body(%Qubit* null)
call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*))
call void @__quantum__qis__ccx__body(%Qubit* null, %Qubit* inttoptr (i64 1 to %Qubit*), %Qubit* inttoptr (i64 2 to %Qubit*))
call void @__quantum__qis__mz__body(%Qubit* null, %Result* null)
call void @__quantum__qis__mz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*))
call void @__quantum__qis__mz__body(%Qubit* inttoptr (i64 2 to %Qubit*), %Result* inttoptr (i64 2 to %Result*))
call void @__quantum__rt__array_record_output(i64 3, i8* null)
call void @__quantum__rt__result_record_output(%Result* null, i8* null)
call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null)
call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 2 to %Result*), i8* null)
ret void
}

declare void @__quantum__qis__h__body(%Qubit*)

declare void @__quantum__qis__x__body(%Qubit*)

declare void @__quantum__qis__ccx__body(%Qubit*, %Qubit*, %Qubit*)

declare void @__quantum__qis__mz__body(%Qubit*, %Result* writeonly) #1

declare void @__quantum__rt__array_record_output(i64, i8*)

declare void @__quantum__rt__result_record_output(%Result*, i8*)

attributes #0 = { "entry_point" "num_required_qubits"="3" "num_required_results"="3" "output_labeling_schema" "qir_profiles"="custom" }
attributes #1 = { "irreversible" }

!llvm.module.flags = !{!0, !1, !2, !3}

!0 = !{i32 1, !"qir_major_version", i32 1}
!1 = !{i32 7, !"qir_minor_version", i32 0}
!2 = !{i32 1, !"dynamic_qubit_management", i1 false}
!3 = !{i32 1, !"dynamic_result_management", i1 false}

Loading
Loading