unitaryfund / pyqrack

Pure Python bindings for the pure C++11/OpenCL Qrack quantum computer simulator library
MIT License
16 stars 8 forks source link

General help thread #2

Closed WrathfulSpatula closed 2 years ago

WrathfulSpatula commented 3 years ago

The only dependency of pyqrack, being the underlying https://github.com/vm6502q/qrack library, is stable, validated, and relatively quiescent, for now, in my estimation as lead developer. A few months ago, it would have been more difficult to package shared library builds for it, for a project like pyqrack. However, I think it's likely that my upcoming development time on vm6502q will tend be in "superstructure" plugins, at least a good while, like Unity3D, respective "Big 5" framework plugins, and now pyqrack.

So, I want to add this library to the pypi registry, bundled with respective qrack shared library builds appropriate to each wheel. Maybe I can solicit advice from those with more Python experience: what's the best way to do this?

Over in my OpenRelativity fork, for Unity3D, I build an excessive number of OS/CPU combination shared library versions of qrack. These include the following:

  1. x86_64 Linux
  2. ARMv7 Linux
  3. ARM64 Liinux
  4. x86_64 Mac
  5. ARM64 iOS ("Any iOS device," in XCode)
  6. x86_64 Windows
  7. x86 Windows
  8. ARMv7 Android
  9. ARM64 Android
  10. x86 Android
  11. WASM

... I think that's it, so far. In fact, I don't plan to update qrack code, right now, so I think I'm about to build all of the above again, right now, just as part of my development cycle. For the python package, for those platforms that actually support GPU, I'd probably also like to build separate CPU/GPU binary packages, potentially. The GPU binaries actually will automatically detect if 0 OpenCL devices are available, and fall back to CPU-only defaults, (without needing to link any more OpenCL in qrack user code, by the way, if we're at C++ level). However, initializing the qrack OpenCL environment, to perform this check, or just in general, adds a significant one-time delay to scripts, every time the shared library is loaded, and I'd like people to be able to bypass this.

Who can suggest the best way to package these, for pypi? (I'll do it all myself, if necessary, and track progress in this issue.)

CMOT09 commented 3 years ago

ฉันต้องการความช่วยเหลือและแนะแนวทางที่ถูกต้องเพราะฉันยังไม่เข้าใจเกี่ยวกับบัญชีและความเป็นมาของงานที่ไดรับหมอบหมายหรือสร้างขึ้น ช่วยด้วยครับฉันไม่เข้าใจและไม่ได้ต้องการให้เกิดเรื่องวุ่นขึ้นเลย

WrathfulSpatula commented 3 years ago

(For anyone unfamiliar with Thai or Hmong, like me, here's the Google translate, sorry:)

I need help and proper guidance because I still don't understand the account and the background of the assignments or creations. Help, I don't understand and I don't want to mess things up at all.

Thank you! This project, PyQrack, is just a pure ctypes wrapper on https://github.com/vm6502q/qrack, my quantum computer simulator library, at the core of my "vm6502q" quantum computing framework.

The "shared library" (.so or .dll) is here: https://github.com/vm6502q/qrack/blob/main/include/pinvoke_api.hpp https://github.com/vm6502q/qrack/blob/main/src/pinvoke_api.cpp

If one follows the instructions in the README of vm6502q/qrack, you currently have to build and install vm6502q/qrack, to use PyQrack.

In the qrack repository directory:

$ mkdir _build && cd _build && cmake .. && make all install

When you make all install, you can now also use PyQrack, by checking out the PyQrack repository and pip install ., for local install.

The point of this issue, #2, is that I want to package PyQrack with the Qrack binary already in it, on the pypi registry, but there are many potential build variants. :smile:

CMOT09 commented 3 years ago

ผมต้องเริ่มจากตรงไหนครับรบกวนขอคำแนะนำด้วยครับ

WrathfulSpatula commented 3 years ago

This is what I read:

Where do I have to start? Please give me some advice.

The first step is to build and install the vm6502q/qrack shared library and static library, with CMake. The second step is pip install . in the vm6502q/pyqrack directory. Those are the only steps.

The more detailed explanation follows:

Start at the vm6502q/qrack README. You only need, in vm6502q/qrack, to make a ./_build directory:

$ mkdir _build && cd _build && cmake .. && make all install

This builds the vm6502/qrack shared library (Linux) libqrack_pinvoke.so or (Windows) qrack_pinvoke.dll. The shared library accesses the static library, libqrack.a. The command above makes those files and puts them in /usr/local/include/qrack and /usr/local/lib/qrack. On Windows, the files are installed in C:\Program Files\qrack. (I don't remember if that's the exact Microsoft Windows directory.)

Just run the build and install command written above, to do all that. It's the first step.

Then, clone vm6502q/pyqrack, and type:

pip install .

otherwise

python3 -m pip install .

or otherwise.

The point is to locally install the Python package with the shared library and static library. We have not yet published the Python package, but this is open source software under the MIT License. (vm6502q/qrack is under the LGPL License, but it's free to link against the library for MIT License.)

WrathfulSpatula commented 3 years ago

Sorry for the typos. I'm trying to speak simply and plainly for translation, but it's hard for me to understand.

WrathfulSpatula commented 3 years ago

I hope that worked. :smile:

Anyone, tell me if you need help, and I'll try my best. :+1:

WrathfulSpatula commented 3 years ago

By the way, run

user@domain:qrack/_build$ qrack_cl_precompile

or, after installation, run $ qrack_cl_precompile from anywhere.

This compiles the OpenCL kernels and saves the result in the ~/.qrack directory. Then, you don't have to compile the kernels again every time you run a program.

The compilation might fail. If the compilation doesn't succeed or breaks anything, just delete the ~/.qrack directory. But, it's better if you can compile the OpenCL kernels beforehand.

WrathfulSpatula commented 3 years ago

Here's an example of usage:

from pyqrack import QrackSimulator

test = QrackSimulator(qubitCount=4)
test.x(0)
test.x(1)
test.mcx([1], 2)
test.h(3)
print(test.measure_shots([0,1,2,3], 10))

The qubitCount=4 parameter creates 4 "qubits." You can allocate more qubits with allocate_qubit().

By design, you just call quantum gate instruction methods, without thinking about it too hard. vm6502q/qrack is basically an optimizing quantum compiler (simulator). Qrack will optimize your sequence of quantum gate instructions on-the-fly, as best it can.

For example, if you write a pure Clifford group or "stabilizer" quantum circuit, per Gottesman-Knill theorem, Qrack should just execute it efficiently, without needing to be told that the circuit is Clifford group.

WrathfulSpatula commented 3 years ago

Much easier, now:

$ pip install pyqrack

It is an alpha release version. However, it will probably work. :smile: :+1: