Skip to content

A library of functions for mathematical structures used in Quantum computing

License

Notifications You must be signed in to change notification settings

rsdc2/qsharp-math-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Q# Utilties

This is a utility library I am writing as I learn Q# and quantum programming. It comprises functions I have found helpful that do not currently appear (as far as I can see) in the standard library. Functions cover the following areas:

  • Complex numbers: Complex.qs, namespace Utilities.Complex
  • Vectors: Vectors.qs, namespace Utilities.Vectors
  • Matrices: Matrices.qs and the Matrices folder, namespace Utilities.Matrices
  • Higher order functions: HigherOrderFunctions.qs

Running

With VSCode and the QDK

The Q# part of the program can be run in VSCode provided that the Azure Quantum Development Kit (QDK) is installed.

Calling Q# from Python

Testing

The tests are written in Python and run with pytest. To install Python with its dependencies (qsharp, numpy and pytest) (and a currently-empty package qsharp-utilitiies):

  1. Clone or download the repository.

  2. Navigate into the cloned / downloaded repository.

In a virtual environment (recommended)

  1. Make sure the virtual environment has been activated, e.g. on Linux:

    source env/bin/activate
  2. Install with pip:

    pip install .

At the user level

  1. Alternatively, from within the cloned repository, install at the user level with:

    pip install . --user

With dev dependencies (in a virtual environment)

  1. If you wish to install mypy for type checking (in addition to the other dependencies), run:

    pip install .[dev]

Using this library as a dependency in your project

You can use this library as a dependency in your own project. To do this, your qsharp.json file should (minimally) contain the following information:

{
    "author": "your-name-here",
    "dependencies": {
        "QsUtils": {
            "path": "path/to/dependency"
        }
    }
}

To import from this library, prefix the module path you wish to import with the name for your dependency, in this case QsUtils, e.g.:

import Std.Math.*;
import QsUtils.Utilities.Complex.*;

@EntryPoint()
function Main() : Complex {
    ToC(1.)
}

Full instructions on project structure and using dependencies can be found in the Microsoft Docs.

Functions

Complex numbers

Conjugate(complex : Complex) : Complex

Produces the complex conjugate of input parameter complex.

EqualC(x : Complex, y : Complex) : Bool

Complex numbers cannot be directly compared. Thus the following expression produces a type error, since the Complex struct does not support equality:

Complex(1., 1.) == Complex(1., 1.)

The EqualC function performs this comparison:

EqualC(Complex(1., 1.), Complex(1., 1.)); // Evaluates to true

Higher order functions

Curry2<'T, 'U, 'V>(f : ('T, 'U) -> 'V) : ('T -> 'U -> 'V)

Takes a function with a pair of parameters, and returns a curried version of it, i.e. two functions taking a single parameter each. For example, whereas EqualC has the type signature:

(Complex, Complex) -> Bool

in the following:

let equalC = Curry2(EqualC);

equalC has the type signature:

Complex -> Complex -> Bool

Dependencies and licenses

Q# itself is licensed under MIT.

Python dependencies are the following:

Licenses for these dependencies are included in the LICENSES directory.

About

A library of functions for mathematical structures used in Quantum computing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published