Git Product home page Git Product logo

qcc4cp / qcc Goto Github PK

View Code? Open in Web Editor NEW
83.0 11.0 25.0 2.44 MB

Source code for the book "Quantum Computing for Programmers", Cambridge University Press

License: Apache License 2.0

Starlark 1.68% Python 33.46% C++ 64.60% Shell 0.08% Dockerfile 0.19%
quantum-computing factorization grovers-algorithm shors-algorithm deutsch-algorithm deutsch-jozsa-algorithm python solovay-kitaev spectral-decomposition quantum-fourier-transform

qcc's Introduction

Quantum Computing for Programmers

This is the open-source repository for the book Quantum Computing for Programmers by Robert Hundt, Cambridge University Press (QCC4CP for short). The book describes this implementation in great detail, including all the underlying math and derivations. Note, however, that this code base is evolving - not all algorithms found here are discussed in the book.

To get started quickly on the Python sources, you may find the Quickstart Guide helpful.

This project builds vendor-independent infrastructure from the ground up and implements standard algorithms, such as Quantum Teleportation, Superdense coding, Deutsch-Jozsa, Bernstein-Vazirani, Quantum Phase estimation (QPE), Grover's Search (with application to Quantum counting, amplitude estimation, Mean and Median estimation, 3SAT, Graph Coloring, and Minimum finding), Quantum random walks, VQE, Max-Cut, Subset-Sum, Quantum Fourier Transform (QFT), Shor's integer factorization, Solovay-Kitaev, Principal Component Analysis, and a few more. It also implements high performance quantum simulation and a transpilation technique to compile circuits to other infrastructures, such as Qiskit or Cirq.

The code is organized as follows:

  • src is the main source directory. All algorithms are in this directory.
  • src/lib contains the library functions for tensors, states, operators, circuits, and so on, as well as their corresponding tests. All algorithms depend on these library functions.
  • src/libq contains the sparse implementation.
  • src/benchmarks contains a few benchmarks, as they are mentioned in the book.
  • resources contains additional text, sections and chapters.
  • errata contains the errata for the book - corrections and clarifications.
  • external contains the *.BUILD files to point bazel to python and numpy.

Installation

There are several ways to get started on this code base:

Run

The main algorithms are all in src. To run individual algorithms via bazel, run any of these command lines. Note the missing .py extensions when using bazel). Alternatively, run each Python file with python <file> (PYTHONPATH must point to the root directory):

# Algorithms discussed in the book:
   bazel run arith_classic
   bazel run arith_quantum
   bazel run bernstein
   bazel run counting
   bazel run deutsch
   bazel run deutsch_jozsa
   bazel run entanglement_swap
   bazel run grover
   bazel run max_cut
   bazel run order_finding
   bazel run phase_estimation
   bazel run phase_kick
   bazel run quantum_walk
   bazel run shor_classic
   bazel run simon
   bazel run simon_general
   bazel run solovay_kitaev
   bazel run subset_sum
   bazel run superdense
   bazel run supremacy
   bazel run swap_test
   bazel run teleportation
   bazel run vqe_simple

# Additional algorithms and techniques, to clarify, or
# in preparation of a new edition of the book:
   bazel run amplitude_estimation
   bazel run bell_basis
   bazel run chsh
   bazel run estimate_pi
   bazel run euclidean_distance
   bazel run graph_coloring
   bazel run hadamard_test
   bazel run hamiltonian_encoding
   bazel run hhl
   bazel run hhl_2x2
   bazel run inversion_test
   bazel run minimum_finding
   bazel run oracle_synth
   bazel run pauli_rep
   bazel run purification
   bazel run qram
   bazel run quantum_mean
   bazel run quantum_median
   bazel run quantum_pca
   bazel run sat3
   bazel run schmidt_decomp
   bazel run spectral_decomp
   bazel run state_prep
   bazel run state_prep_mottonen
   bazel run zy_decomp

To test aspects of the sparse implementation:

  cd src/libq
  bazel test ...

To run the benchmarks:

  cd src/benchmarks
  bazel run larose_benchmark
  bazel run tensor_math

Transpilation

To experiment with transpilation, a few things must work together:

  • Specify a target output. For example, to generate a libq C++ file, use --libq=./test.cc

  • The code should only contain a single circuit.qc()-generated circuit. This circuit will not be eagerly executed. Instead, all gates and qubits will be collected in an internal IR.

  • There must be a single call to qc.dump_to_file(). The circuit as that point will be transpiled to the target platform (an example of this can be found in order_finding.py).

For the given example, the generated file test.cc can be compiled and linked with libq with a command-line similar to this one:

$ cd qcc/src
$ cc -O2 -Ilibq test.cc libq/qureg.cc libq/apply.cc libq/gates.cc -o a.out -lc++
$ a.out

About

This code and book were written by Robert Hundt. At the time of this writing, Robert is a Distinguished Enginer at Google. However, this is a private project, developed on personal infrastructure and in private time. It is completely independent of Robert's work at Google.

Reach Robert at

Additional Thanks

  • Colin Zhu, for pointing out coding problems.
  • Kevin Crook, Univ. of CA, Berkeley, for feedback and discussion of the Chinese Remainder Theorem.
  • Moez A. AbdelGawad, Alexandria University, Egypt, for suggesting Windows and SageMath ports.
  • Stefanie Scherzinger, Universitaet Passau, for corrections and suggesting Docker.
  • Abdolhamid Pourghazi and Stefan Klessinger, for providing and maintaining the Dockerfile.
  • Michael Broughton, for help with purification.
  • Mikhail Remnev, for pointing out a .dylib problem in MacOS
  • Andrea Novellini, for fixing a WORKSPACE issue with bazel 7.0.x

qcc's People

Contributors

hundt98847 avatar qcc4cp avatar rhundt avatar xbe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qcc's Issues

Bazel ERROR

Hello,

After following the instructions for compiling the C++ code in Docker, I get the following Bazel error:

`$ docker build -t qcc4cp:latest .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/

Sending build context to Docker daemon 220.1MB
Step 1/27 : FROM debian:11
---> 873181a41b99
Step 2/27 : LABEL maintainer="Abdolhamid Pourghazi [email protected]"
---> Using cache
---> 883cfe428df3
Step 3/27 : LABEL maintainer="Stefan Klessinger [email protected]"
---> Using cache
---> 1bba1a411a0b
Step 4/27 : ENV DEBIAN_FRONTEND noninteractive
---> Using cache
---> de5d6fb41216
Step 5/27 : ENV LANG="C.UTF-8"
---> Using cache
---> 1379171292b9
Step 6/27 : ENV LC_ALL="C.UTF-8"
---> Using cache
---> f3bd7ed972c8
Step 7/27 : RUN apt-get update && apt-get install -y --no-install-recommends build-essential git libpython3-dev python3 python3-pip wget
---> Using cache
---> c058f7ccc312
Step 8/27 : RUN python3 -m pip install absl-py numpy scipy
---> Using cache
---> 3ae8eb401763
Step 9/27 : RUN useradd -m -G sudo -s /bin/bash repro && echo "repro:repro" | chpasswd
---> Using cache
---> b6b00da09dd4
Step 10/27 : USER repro
---> Using cache
---> eb773b7aa90a
Step 11/27 : RUN mkdir -p /home/repro/bin/
---> Using cache
---> 5119a047c1a3
Step 12/27 : RUN wget -O /home/repro/bin/bazel https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64
---> Using cache
---> a7f664877276
Step 13/27 : RUN chmod +x /home/repro/bin/bazel
---> Using cache
---> 8c4c771990c8
Step 14/27 : ENV PATH="/home/repro/bin/:${PATH}"
---> Using cache
---> c51c34440086
Step 15/27 : RUN mkdir -p /home/repro/sources/
---> Using cache
---> 3a898b65d78f
Step 16/27 : WORKDIR /home/repro/sources/
---> Using cache
---> 8439b2897039
Step 17/27 : RUN git clone https://github.com/qcc4cp/qcc.git
---> Using cache
---> ba3894a520d1
Step 18/27 : WORKDIR /home/repro/sources/qcc
---> Using cache
---> 58b7d18fe6fb
Step 19/27 : RUN sed -i 's/python3.7/python3.9/g' WORKSPACE
---> Using cache
---> 67ee9c8e5681
Step 20/27 : WORKDIR /home/repro/sources/qcc/src/lib
---> Using cache
---> dfca1d6daa5d
Step 21/27 : RUN bazel build all
---> Running in d09b99177dde
2023/12/18 14:16:12 Downloading https://releases.bazel.build/7.0.0/release/bazel-7.0.0-linux-x86_64...
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
Computing main repo mapping:
WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to bazelbuild/bazel#18958.
Computing main repo mapping:
Computing main repo mapping:
ERROR: /home/repro/sources/qcc/WORKSPACE:1:21: //external:third_party_python: invalid label '/home/repro/sources/qcc/python.BUILD' in attribute 'build_file' in 'new_local_repository' rule: invalid target name '/home/repro/sources/qcc/python.BUILD': target names may not start with '/'
ERROR: /home/repro/sources/qcc/WORKSPACE:8:21: //external:third_party_numpy: invalid label '/home/repro/sources/qcc/numpy.BUILD' in attribute 'build_file' in 'new_local_repository' rule: invalid target name '/home/repro/sources/qcc/numpy.BUILD': target names may not start with '/'
ERROR: Error computing the main repository mapping: Encountered error while reading extension file 'toolchains/local_java_repository.bzl': no such package '@@rules_java_builtin//toolchains': error loading package 'external': Could not load //external package
The command '/bin/sh -c bazel build all' returned a non-zero code: 1
$ `

I get the exact same error when running the compilation outside of Docker following README.Linux.md. I am on Ubuntu 22.04.

I am not a Bazel user and Google so far didn't help.

Bazel build ERROR on ubuntu

I am using Linux Ubuntu 22.04.3 LTS (WSL 2) on a windows 11 machine and I have python 3.10, numpy 1.26.3 and bazel 7.0.0 installed.
I modified the WORKSPACE script according to my path to python and numpy, which are respectively:
/usr/include/python3.10
/usr/local/lib/python3.10/dist-packages/numpy/core/

However, when I go in qcc/src/lib and try to build using bazel build all I got this error:

ERROR: /home/username/qcc/WORKSPACE:1:21: //external:third_party_python: invalid label '/home/username/qcc/python.BUILD' in attribute 'build_file' in 'new_local_repository' rule: invalid target name '/home/username/qcc/python.BUILD': target names may not start with '/'
ERROR: /home/username/qcc/WORKSPACE:8:21: //external:third_party_numpy: invalid label '/home/username/qcc/numpy.BUILD' in attribute 'build_file' in 'new_local_repository' rule: invalid target name '/home/username/qcc/numpy.BUILD': target names may not start with '/'
ERROR: Error computing the main repository mapping: Encountered error while reading extension file 'toolchains/jdk_build_file.bzl': no such package '@@rules_java_builtin//toolchains': error loading package 'external': Could not load //external package

SOLUTION

My solution was the following:

  • in the main folder qcc, create a folder called external:
  • move python.BUILD and numpy.BUILD from qcc to qcc/external
  • then modify the WORKSPACE script as follows:
new_local_repository(
    name = "third_party_python",
    build_file = "//external:python.BUILD",
    path = "/usr/include/python3.10",
)

new_local_repository(
    name = "third_party_numpy",
    build_file = "//external:numpy.BUILD",
    path = "/usr/local/lib/python3.10/dist-packages/numpy/core/",
)

This solved the issue for me.
Hope this was helpful, have a greate day :)

Compiling lilxgates in MacBook Air M2

Hi!

I tried to run qcc simulator on a MacBook Air with the processor M2. The macOS is Ventura 13.5.2. It works correctly. The python was installed though brew and downloaded by git.

But I can't run it with C++ accelerator, the module returns the message:

**************************************************************`
  WARNING: Could not find 'libxgates.so'.
  Please build it and point PYTHONPATH to it.
  Execution is being re-directed to a Python implementation,
  performance may suffer greatly.
  **************************************************************`

All paths are corrected according libraries place that file WORKSPACE takes into accounts the python positions:

new_local_repository(
    name = "third_party_python",
    build_file = __workspace_dir__ + "/python.BUILD",
    # Configure:
    path = "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers/",
)

new_local_repository(
    name = "third_party_numpy",
    build_file = __workspace_dir__ + "/numpy.BUILD",
    # Configure:
    path = "/Users/maremnev/Library/Python/3.9/lib/python/site-packages/numpy/core/",
)

And variables PATH and PYTHONPATH are corrected too:

export PATH=/Users/maremnev/WorkPlace/qcc:$PATH
export PYTHONPATH=/Users/maremnev/WorkPlace/qcc:$PYTHONPATH

export PYTHONPATH=/Users/maremnev/WorkPlace/qcc/bazel-bin/src/lib:$PYTHONPATH

Command bazel build all returns no errors. But it made in directory qcc/bazel-bin/src/lib files libxgates.a and libxgates.dylib, but python searchs for file libxgates.so.

I guess it's a remainder from Linux.

How to compile the library correctly in Mac? May be it's needed to point the another library in python?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.