libs.math

A tiny math library which will be extended thoughout the next weeks. Right now, it only contains the math functions necessary to run Beauregard’s implementation of Shor’s algorithm.

projectq.libs.math._constantmath
projectq.libs.math._default_rules Registers a few default replacement rules for Shor’s algorithm to work (see Examples).
projectq.libs.math._gates
projectq.libs.math.AddConstant(a) Add a constant to a quantum number represented by a quantum register, stored from low- to high-bit.
projectq.libs.math.AddConstantModN(a, N) Add a constant to a quantum number represented by a quantum register modulo N.
projectq.libs.math.all_defined_decomposition_rules Built-in mutable sequence.
projectq.libs.math.MultiplyByConstantModN(a, N) Multiply a quantum number represented by a quantum register by a constant modulo N.
projectq.libs.math.SubConstant(a) Subtract a constant from a quantum number represented by a quantum register, stored from low- to high-bit.
projectq.libs.math.SubConstantModN(a, N) Subtract a constant from a quantum number represented by a quantum register modulo N.

Submodules

_constantmath

projectq.libs.math._constantmath.add_constant(eng, c, quint)[source]

Adds a classical constant c to the quantum integer (qureg) quint using Draper addition.

Note: Uses the Fourier-transform adder from
https://arxiv.org/abs/quant-ph/0008033.
projectq.libs.math._constantmath.add_constant_modN(eng, c, N, quint)[source]

Adds a classical constant c to a quantum integer (qureg) quint modulo N using Draper addition and the construction from https://arxiv.org/abs/quant-ph/0205095.

projectq.libs.math._constantmath.inv_mod_N(a, N)[source]
projectq.libs.math._constantmath.mul_by_constant_modN(eng, c, N, quint_in)[source]

Multiplies a quantum integer by a classical number a modulo N, i.e.,

|x> -> |a*x mod N>

(only works if a and N are relative primes, otherwise the modular inverse does not exist).

_default_rules

Registers a few default replacement rules for Shor’s algorithm to work (see Examples).

_gates

class projectq.libs.math._gates.AddConstant(a)[source]

Add a constant to a quantum number represented by a quantum register, stored from low- to high-bit.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[1] # qunum is now equal to 2
AddConstant(3) | qunum # qunum is now equal to 5
get_inverse()[source]

Return the inverse gate (subtraction of the same constant).

class projectq.libs.math._gates.AddConstantModN(a, N)[source]

Add a constant to a quantum number represented by a quantum register modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[1] # qunum is now equal to 2
AddConstantModN(3, 4) | qunum # qunum is now equal to 1

Note

Pre-conditions:

  • c < N
  • c >= 0
  • The value stored in the quantum register must be lower than N
get_inverse()[source]

Return the inverse gate (subtraction of the same number a modulo the same number N).

class projectq.libs.math._gates.MultiplyByConstantModN(a, N)[source]

Multiply a quantum number represented by a quantum register by a constant modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[2] # qunum is now equal to 4
MultiplyByConstantModN(3,5) | qunum # qunum is now 2.

Note

Pre-conditions:

  • c < N
  • c >= 0
  • gcd(c, N) == 1
  • The value stored in the quantum register must be lower than N
projectq.libs.math._gates.SubConstant(a)[source]

Subtract a constant from a quantum number represented by a quantum register, stored from low- to high-bit.

Parameters:a (int) – Constant to subtract

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[2] # qunum is now equal to 4
SubConstant(3) | qunum # qunum is now equal to 1
projectq.libs.math._gates.SubConstantModN(a, N)[source]

Subtract a constant from a quantum number represented by a quantum register modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Parameters:
  • a (int) – Constant to add
  • N (int) – Constant modulo which the addition of a should be carried out.

Example

qunum = eng.allocate_qureg(3) # 3-qubit number
X | qunum[1] # qunum is now equal to 2
SubConstantModN(4,5) | qunum # qunum is now -2 = 6 = 1 (mod 5)

Note

Pre-conditions:

  • c < N
  • c >= 0
  • The value stored in the quantum register must be lower than N

Module contents

class projectq.libs.math.AddConstant(a)[source]

Add a constant to a quantum number represented by a quantum register, stored from low- to high-bit.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[1] # qunum is now equal to 2
AddConstant(3) | qunum # qunum is now equal to 5
__init__(a)[source]

Initializes the gate to the number to add.

Parameters:a (int) – Number to add to a quantum register.

It also initializes its base class, BasicMathGate, with the corresponding function, so it can be emulated efficiently.

get_inverse()[source]

Return the inverse gate (subtraction of the same constant).

class projectq.libs.math.AddConstantModN(a, N)[source]

Add a constant to a quantum number represented by a quantum register modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[1] # qunum is now equal to 2
AddConstantModN(3, 4) | qunum # qunum is now equal to 1

Note

Pre-conditions:

  • c < N
  • c >= 0
  • The value stored in the quantum register must be lower than N
__init__(a, N)[source]

Initializes the gate to the number to add modulo N.

Parameters:
  • a (int) – Number to add to a quantum register (0 <= a < N).
  • N (int) – Number modulo which the addition is carried out.

It also initializes its base class, BasicMathGate, with the corresponding function, so it can be emulated efficiently.

get_inverse()[source]

Return the inverse gate (subtraction of the same number a modulo the same number N).

class projectq.libs.math.MultiplyByConstantModN(a, N)[source]

Multiply a quantum number represented by a quantum register by a constant modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[2] # qunum is now equal to 4
MultiplyByConstantModN(3,5) | qunum # qunum is now 2.

Note

Pre-conditions:

  • c < N
  • c >= 0
  • gcd(c, N) == 1
  • The value stored in the quantum register must be lower than N
__init__(a, N)[source]

Initializes the gate to the number to multiply with modulo N.

Parameters:
  • a (int) – Number by which to multiply a quantum register (0 <= a < N).
  • N (int) – Number modulo which the multiplication is carried out.

It also initializes its base class, BasicMathGate, with the corresponding function, so it can be emulated efficiently.

projectq.libs.math.SubConstant(a)[source]

Subtract a constant from a quantum number represented by a quantum register, stored from low- to high-bit.

Parameters:a (int) – Constant to subtract

Example

qunum = eng.allocate_qureg(5) # 5-qubit number
X | qunum[2] # qunum is now equal to 4
SubConstant(3) | qunum # qunum is now equal to 1
projectq.libs.math.SubConstantModN(a, N)[source]

Subtract a constant from a quantum number represented by a quantum register modulo N.

The number is stored from low- to high-bit, i.e., qunum[0] is the LSB.

Parameters:
  • a (int) – Constant to add
  • N (int) – Constant modulo which the addition of a should be carried out.

Example

qunum = eng.allocate_qureg(3) # 3-qubit number
X | qunum[1] # qunum is now equal to 2
SubConstantModN(4,5) | qunum # qunum is now -2 = 6 = 1 (mod 5)

Note

Pre-conditions:

  • c < N
  • c >= 0
  • The value stored in the quantum register must be lower than N