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

merge dev into master #27

Merged
merged 82 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
617aed9
1. Optimized data convertion between python and cpp 2. Fixed set_num …
Zhaoyilunnn Apr 1, 2023
b34b40a
Add destructor to Statevector
Zhaoyilunnn Apr 2, 2023
5abb1a7
Merge pull request #11 from Zhaoyilunnn/master
lss0208 Apr 2, 2023
ad5ef8c
add GPU support
lss0208 Apr 3, 2023
62c759c
rm submodule
lss0208 Apr 3, 2023
23bfc07
add func doc
lss0208 Apr 3, 2023
470891e
fix exceptions
lss0208 Apr 3, 2023
e023a88
use unique_ptr
lss0208 Apr 7, 2023
fd26486
fix cmake
lss0208 Apr 7, 2023
0d9edcd
增加了dag
Apr 24, 2023
0833790
Merge pull request #13 from fatboy1994/dev
YuChenSSR Apr 24, 2023
4a1c6bb
add dag
Apr 24, 2023
8fe4697
Merge pull request #14 from fatboy1994/dev
YuChenSSR Apr 24, 2023
53b00a2
add dag , modify the test book
YuChenSSR Apr 24, 2023
8344afe
change the explaination
YuChenSSR Apr 24, 2023
4ac5486
Merge pull request #15 from fatboy1994/dev
YuChenSSR Apr 24, 2023
ce0d2f4
optimize base
lss0208 Apr 25, 2023
83e6370
Merge branch dev
lss0208 Apr 25, 2023
745b731
优化了gate_to_node代码
YuChenSSR Apr 25, 2023
258c0e3
Merge pull request #17 from YuChenSSR:dev
YuChenSSR Apr 25, 2023
4c090c0
add parser;modify pulse
lss0208 Apr 27, 2023
a582cf7
small fix
lss0208 Apr 27, 2023
04d64d6
Merge branch 'dev' of https://github.com/ScQ-Cloud/pyquafu into dev
lss0208 Apr 27, 2023
c593262
Added parameters for the pulse gate
YuChenSSR May 6, 2023
81a35b5
Merge pull request #18 from YuChenSSR:dev
YuChenSSR May 6, 2023
768d61a
Add runtime job id in task object
Zhaoyilunnn May 26, 2023
36887fe
fix: load account should use url from configuration file
Zhaoyilunnn Jun 7, 2023
b4327d4
fix 0: parser error: TypeError: InstructionNode.__init__() missing 2…
Zhaoyilunnn Jun 7, 2023
93edf5b
Merge pull request #20 from Zhaoyilunnn/master
ScQ-Cloud Jun 8, 2023
a32de95
fix: default runtime_job_id should be empty string
Zhaoyilunnn Jun 8, 2023
9599f02
Merge pull request #24 from Zhaoyilunnn/master
Zhaoyilunnn Jun 8, 2023
7c8a43a
update DAGCircuit class and some tests
YuChenSSR Jun 29, 2023
4cdf2c5
Merge pull request #25 from YuChenSSR/dev
YuChenSSR Jun 29, 2023
adbce89
Add files via upload
chensgit169 Jul 2, 2023
a5b5c0b
Add files via upload
chensgit169 Jul 2, 2023
1e14e8e
Delete visualisation directory
chensgit169 Jul 2, 2023
878b729
"adjusted format according to PEP8"
chensgit169 Jul 5, 2023
0c3b06f
"adjusted format according to PEP8"
chensgit169 Jul 5, 2023
244e08d
"optimized import in quantum_circuit.py"
chensgit169 Jul 5, 2023
94515ca
"add sxdg and related"
chensgit169 Jul 5, 2023
a59a82b
"add some user-friendly features"
chensgit169 Jul 6, 2023
5c1f617
"Separate element_gates.py"
chensgit169 Jul 6, 2023
8e01805
"Modify measure()"
chensgit169 Jul 6, 2023
dde1609
"Isolate matrices of gates in thus file. "
chensgit169 Jul 6, 2023
4f63c22
"delete old element_gates.py"
chensgit169 Jul 6, 2023
16572de
"ignore .pyd in git"
chensgit169 Jul 11, 2023
ec56ecb
"Add Superclass Instruction"
chensgit169 Jul 11, 2023
edade0c
"Fix some small problems."
chensgit169 Jul 11, 2023
313dece
"Add an example for plotting."
chensgit169 Jul 11, 2023
996fecd
"Move pulse into element package, inherent Instruction"
chensgit169 Jul 11, 2023
099082d
"Set `name` and `matrix` properties for Ins and Gates"
chensgit169 Jul 11, 2023
ed2655e
"Add PLY into requirements.txt"
chensgit169 Jul 11, 2023
d1cba7e
"Fix the para-missing problem for rotation gate"
chensgit169 Jul 11, 2023
c69a61d
add unitary decomposer
chensgit169 Jul 12, 2023
ef4b7ca
integrate unitary-decomposer into QuantumCircuit as an user function …
chensgit169 Jul 12, 2023
e3c5fdd
fix coding-time problem of matrices
chensgit169 Jul 12, 2023
85faea1
open benchmark module
chensgit169 Jul 12, 2023
c8e4810
detailing QuafuError (unfinished)
chensgit169 Jul 12, 2023
3f7e737
small adjustment
chensgit169 Jul 12, 2023
1488317
small adjustment in formatting
chensgit169 Jul 12, 2023
9b0adc3
add register mechanism for QuantumGate
chensgit169 Jul 12, 2023
47a085a
add register mechanism for Instruction.
chensgit169 Jul 12, 2023
5963397
modify time_func as an attribute of QuantumPulse.
chensgit169 Jul 12, 2023
4394453
small fix
chensgit169 Jul 12, 2023
d621f10
small fix
chensgit169 Jul 12, 2023
35ec52c
standardize __init__() of instruction.py
chensgit169 Jul 12, 2023
a55094d
small adjustment
chensgit169 Jul 12, 2023
cb6be26
circuitPlot.py almost finished (untested yet)
chensgit169 Jul 12, 2023
11639de
fix some problem during benchmarking
chensgit169 Jul 13, 2023
c2b0532
add cx() as alley of cnot()
chensgit169 Jul 14, 2023
fd7a3ff
fix 2 known problem, add another testbench
chensgit169 Jul 14, 2023
59faa18
fix 2 known problem, add another testbench
chensgit169 Jul 14, 2023
fe65df8
Add iswap() and its plotting.
chensgit169 Jul 20, 2023
28f7022
delete local example in visualisation
chensgit169 Jul 20, 2023
5c72e98
Merge pull request #26 from chensgit169/dev
Zhaoyilunnn Jul 20, 2023
1305073
update v0.3.0
Zhaoyilunnn Jul 21, 2023
95ec9b4
Update __init__.py
chensgit169 Jul 21, 2023
b9a9113
Update deutsch_jozsa.py
chensgit169 Jul 21, 2023
a8f2056
Update circuit_error.py
chensgit169 Jul 21, 2023
18550dd
Delete dag.dot
chensgit169 Jul 21, 2023
5a2e216
Delete dag_test.ipynb
chensgit169 Jul 21, 2023
c6403b5
Delete dagcircuit_test.ipynb
chensgit169 Jul 21, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
pull_request_review:
types: [submitted, edited]
workflow_dispatch:
release:



jobs:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ cmake
*.egg-info
test
.vscode
thirdparty
thirdparty
.pyd
MANIFEST.in
56 changes: 50 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

cmake_minimum_required(VERSION 3.14...3.22)

project(qfvm LANGUAGES CXX C)
project(qfvm LANGUAGES CXX C)

set (CMAKE_BUILD_TYPE Release)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_ARCHITECTURES 70;75;80;90)
if(SKBUILD)

execute_process(
Expand Down Expand Up @@ -42,9 +43,6 @@ ExternalProject_Add(Eigen3
PREFIX ${EIGEN3_ROOT}
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.3.9
# CONFIGURE_COMMAND cd ${EIGEN3_ROOT}/src/Eigen3 && cmake -B build -DCMAKE_INSTALL_PREFIX=${EIGEN3_ROOT}
# BUILD_COMMAND ""
# INSTALL_COMMAND cd ${EIGEN3_ROOT}/src/Eigen3 && cmake --build build --target install

CONFIGURE_COMMAND ""
BUILD_COMMAND ""
Expand Down Expand Up @@ -72,6 +70,7 @@ if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_HOST_SYSTEM_PROCESSOR
endif()
endif()

list (APPEND PRJ_INCLUDE_DIRS src/qfvm)
pybind11_add_module(${PROJECT_NAME} MODULE src/${PROJECT_NAME}/${PROJECT_NAME}.cpp)
add_dependencies(${PROJECT_NAME} Eigen3) #must add dependence for ninja
target_compile_options(${PROJECT_NAME} PUBLIC ${PRJ_COMPILE_OPTIONS})
Expand All @@ -80,4 +79,49 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${PRJ_LIBRARIES})
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ${PYTHON_MODULE_EXTENSION})
target_compile_definitions(${PROJECT_NAME} PRIVATE VERSION_INFO=${PROJECT_VERSION})

install(TARGETS ${PROJECT_NAME} DESTINATION .)
#GPU version
if (USE_GPU)
add_compile_definitions(_USE_GPU)
enable_language(CUDA)
set_source_files_properties(src/${PROJECT_NAME}/${PROJECT_NAME}.cpp PROPERTIES LANGUAGE CUDA)
target_link_libraries(${PROJECT_NAME} PUBLIC cudart)
target_compile_options(${PROJECT_NAME} PUBLIC $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda> )
target_include_directories(${PROJECT_NAME} PUBLIC src/qfvm_gpu)
target_include_directories(${PROJECT_NAME} PUBLIC src/qfvm_gpu/cuda_utils)
target_include_directories(${PROJECT_NAME} PUBLIC ${CUDA_INCLUDE_DIRS})
message("cuda_include" ${CUDA_INCLUDE_DIRS})
if (USE_CUQUANTUM)
add_compile_definitions(_USE_CUQUANTUM)
function(set_with_fallback VARIABLE FALLBACK)
if (NOT DEFINED ${VARIABLE} OR ${VARIABLE} STREQUAL "")
set(${VARIABLE} $ENV{${VARIABLE}} CACHE INTERNAL ${VARIABLE})
if (${VARIABLE} STREQUAL "")
if (NOT ${FALLBACK} STREQUAL "")
set(${VARIABLE} $ENV{${FALLBACK}} CACHE INTERNAL ${VARIABLE})
endif ()
endif ()
endif ()
endfunction()

set_with_fallback(CUSTATEVEC_ROOT CUQUANTUM_ROOT)

if (CUSTATEVEC_ROOT STREQUAL "")
message(FATAL_ERROR "Please set the environment variables CUSTATEVEC_ROOT or CUQUANTUM_ROOT to the path of the cuQuantum installation.")
endif ()

message(STATUS "Using CUSTATEVEC_ROOT = ${CUSTATEVEC_ROOT}")

set(CMAKE_CUDA_FLAGS_ARCH_SM70 "-gencode arch=compute_70,code=sm_70")
set(CMAKE_CUDA_FLAGS_ARCH_SM75 "-gencode arch=compute_75,code=sm_75")
set(CMAKE_CUDA_FLAGS_ARCH_SM80 "-gencode arch=compute_80,code=sm_80 -gencode arch=compute_80,code=compute_80")
set(CMAKE_CUDA_FLAGS_ARCH_SM90 "-gencode arch=compute_90,code=sm_90 -gencode arch=compute_90,code=compute_90")
set(CMAKE_CUDA_FLAGS_ARCH "${CMAKE_CUDA_FLAGS_ARCH_SM70} ${CMAKE_CUDA_FLAGS_ARCH_SM75} ${CMAKE_CUDA_FLAGS_ARCH_SM80} ${CMAKE_CUDA_FLAGS_ARCH_SM90}")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CMAKE_CUDA_FLAGS_ARCH}")

target_include_directories(${PROJECT_NAME} PUBLIC ${CUDA_INCLUDE_DIRS} ${CUSTATEVEC_ROOT}/include)
target_link_directories(${PROJECT_NAME} PUBLIC ${CUSTATEVEC_ROOT}/lib ${CUSTATEVEC_ROOT}/lib64)
target_link_libraries(${PROJECT_NAME} PUBLIC -lcustatevec_static -lcublas )
endif()
endif()

install(TARGETS ${PROJECT_NAME} DESTINATION .)
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ pip install -r requirements.txt
python setup.py install
```

## GPU support
To install PyQuafu with GPU-based circuit simulator, you need build from the source and make sure that [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) is installed. You can run

```
python setup.py install -DUSE_GPU=ON
```
to install the GPU version. If you further have [cuQuantum](https://developer.nvidia.com/cuquantum-sdk) installed, you can install PyQuafu with cuQuantum support.
```
python setup.py install -DUSE_GPU=ON -DUSE_CUQUANTUM=ON
```


## Document
Please see the website [docs](https://scq-cloud.github.io/).

Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ scipy>=1.8.1
setuptools>=58.0.4
sparse>=0.13.0
scikit-build>=0.16.1
pybind11>=2.10.3
pybind11>=2.10.3
ply~=3.11
Pillow~=10.0.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setup(
name="pyquafu",
version="0.2.11",
version="0.3.0",
author="ssli",
author_email="[email protected]",
url="https://github.com/ScQ-Cloud/pyquafu",
Expand All @@ -45,4 +45,4 @@
zip_safe=False,
setup_cfg=True,
license="Apache-2.0 License"
)
)
12 changes: 10 additions & 2 deletions src/qfvm/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Circuit{
private:
uint qubit_num_;
vector<QuantumOperator> gates_{0};

uint max_targe_num_;
public:
Circuit();
explicit Circuit(uint qubit_num);
Expand All @@ -99,6 +99,7 @@ class Circuit{
void add_gate(QuantumOperator &gate);
void compress_gates();
uint qubit_num() const { return qubit_num_; }
uint max_targe_num() const {return max_targe_num_;}
vector<QuantumOperator>gates() const { return gates_; }

};
Expand All @@ -121,16 +122,21 @@ void Circuit::add_gate(QuantumOperator &gate){

Circuit::Circuit(vector<QuantumOperator> &gates)
:
gates_(gates){
gates_(gates),
max_targe_num_(0){
qubit_num_ = 0;
for (auto gate : gates){
for (pos_t pos : gate.positions()){
if (gate.targe_num() > max_targe_num_)
max_targe_num_ = gate.targe_num();
if (pos+1 > qubit_num_){ qubit_num_ = pos+1; }
}
}
}

Circuit::Circuit(py::object const&pycircuit)
:
max_targe_num_(0)
{
auto pygates = pycircuit.attr("gates");
auto used_qubits = pycircuit.attr("used_qubits").cast<vector<pos_t>>();
Expand All @@ -139,6 +145,8 @@ Circuit::Circuit(py::object const&pycircuit)
py::object pygate = py::reinterpret_borrow<py::object>(pygate_h);
QuantumOperator gate = from_pyops(pygate);
if (gate){
if (gate.targe_num() > max_targe_num_)
max_targe_num_ = gate.targe_num();
gates_.push_back(std::move(gate));
}
}
Expand Down
101 changes: 85 additions & 16 deletions src/qfvm/qfvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,111 @@
#include <pybind11/numpy.h>
#include "simulator.hpp"

#ifdef _USE_GPU
#include <cuda_simulator.cuh>
#endif

#ifdef _USE_CUQUANTUM
#include <custate_simu.cuh>
#endif

namespace py = pybind11;

template <typename T>
py::array_t<T> to_numpy(std::vector<T> &&src) {
vector<T>* src_ptr = new std::vector<T>(std::move(src));
auto capsule = py::capsule(src_ptr, [](void* p) { delete reinterpret_cast<std::vector<T>*>(p); });
return py::array_t<T>(
src_ptr->size(), // shape of array
src_ptr->data(), // c-style contiguous strides for vector
capsule // numpy array references this parent
);
}
py::array_t<T> to_numpy(const std::tuple<T*, size_t> &src) {
auto src_ptr = std::get<0>(src);
auto src_size = std::get<1>(src);

auto capsule = py::capsule(src_ptr, [](void* p) {
delete [] reinterpret_cast<T*>(p);
});
return py::array_t<T>(
src_size,
src_ptr,
capsule
);
}

py::object execute(string qasm){
return to_numpy(simulate(qasm).move_data());
return to_numpy(simulate(qasm).move_data_to_python());
}

py::object simulate_circuit(py::object const&pycircuit, vector<complex<double>> const&inputstate){
auto circuit = Circuit(pycircuit);
if (inputstate.size() == 0){
py::object simulate_circuit(py::object const&pycircuit, py::array_t<complex<double>> &np_inputstate){
auto circuit = Circuit(pycircuit);
py::buffer_info buf = np_inputstate.request();
auto* data_ptr = reinterpret_cast<std::complex<double>*>(buf.ptr);
size_t data_size = buf.size;

if (data_size == 0){
StateVector<double> state;
simulate(circuit, state);
return to_numpy(state.move_data());
return to_numpy(state.move_data_to_python());
}
else{
StateVector<double> state{inputstate};
StateVector<double> state(data_ptr, buf.size);
simulate(circuit, state);
return to_numpy(state.move_data());
state.move_data_to_python();
return np_inputstate;
}
}

#ifdef _USE_GPU
py::object simulate_circuit_gpu(py::object const&pycircuit, py::array_t<complex<double>> &np_inputstate){
auto circuit = Circuit(pycircuit);
py::buffer_info buf = np_inputstate.request();
auto* data_ptr = reinterpret_cast<std::complex<double>*>(buf.ptr);
size_t data_size = buf.size;


if (data_size == 0){
StateVector<double> state;
simulate_gpu(circuit, state);
return to_numpy(state.move_data_to_python());
}
else{
StateVector<double> state(data_ptr, buf.size);
simulate_gpu(circuit, state);
state.move_data_to_python();
return np_inputstate;
}
}
#endif

#ifdef _USE_CUQUANTUM
py::object simulate_circuit_custate(py::object const&pycircuit, py::array_t<complex<double>> &np_inputstate){
auto circuit = Circuit(pycircuit);
py::buffer_info buf = np_inputstate.request();
auto* data_ptr = reinterpret_cast<std::complex<double>*>(buf.ptr);
size_t data_size = buf.size;


if (data_size == 0){
StateVector<double> state;
simulate_custate(circuit, state);
return to_numpy(state.move_data_to_python());
}
else{
StateVector<double> state(data_ptr, buf.size);
simulate_custate(circuit, state);
state.move_data_to_python();
return np_inputstate;
}
}
#endif



PYBIND11_MODULE(qfvm, m) {
m.doc() = "Qfvm simulator";
m.def("execute", &execute, "Simulate with qasm");
m.def("simulate_circuit", &simulate_circuit, "Simulate with circuit", py::arg("circuit"), py::arg("inputstate")= py::array_t<complex<double>>(0));

#ifdef _USE_GPU
m.def("simulate_circuit_gpu", &simulate_circuit_gpu, "Simulate with circuit", py::arg("circuit"), py::arg("inputstate")= py::array_t<complex<double>>(0));
#endif

#ifdef _USE_CUQUANTUM
m.def("simulate_circuit_custate", &simulate_circuit_custate, "Simulate with circuit", py::arg("circuit"), py::arg("inputstate")= py::array_t<complex<double>>(0));
#endif
}

8 changes: 4 additions & 4 deletions src/qfvm/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void simulate(Circuit const& circuit, StateVector<data_t> & state){
state.apply_rz(op.positions()[1], op.paras()[0]);
state.apply_cnot(op.positions()[0], op.positions()[1]);
break;


//Other general gate
default:
{
default:
{
if (op.targe_num() == 1){
auto mat_temp = op.mat();
complex<double> *mat = mat_temp.data();
Expand Down Expand Up @@ -176,7 +176,7 @@ void simulate(string qasm, StateVector<double> & state){
StateVector<double> simulate(string qasm){
StateVector<double>state;
simulate(qasm, state);
return std::move(state);
return std::move(state);
}


Loading