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

Quantum gate models for IR integration #16

Merged
merged 78 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
e26a180
add ir
thisac May 20, 2021
ba4be4d
Merge branch 'main' into add-ir
thisac May 20, 2021
5c82fc5
move tests
thisac May 20, 2021
20ff84c
run black
thisac May 20, 2021
a250727
Rename ir/ directories to xir/
Mandrenkov May 21, 2021
c6da8b8
Apply formatter and update import paths
Mandrenkov May 21, 2021
a6cc282
Clarify that top-level makefile is for C++ code
Mandrenkov May 21, 2021
a75f654
Rename Python bindings package to 'bindings'
Mandrenkov May 21, 2021
09a01c0
Move Python bindings tests to subdirectory
Mandrenkov May 21, 2021
471c54a
Update import path to Python bindings module
Mandrenkov May 21, 2021
4d495e1
Rename 'requirements_test.txt' to 'requirements.txt'
Mandrenkov May 21, 2021
a8263ad
Add Python wheel dependencies
Mandrenkov May 21, 2021
1f3c921
Add Python distribution directory to .gitignore
Mandrenkov May 21, 2021
b4d59ad
Create top-level Jet Python package
Mandrenkov May 21, 2021
42d13f0
Create setup.py to build quantum-jet Python wheels
Mandrenkov May 21, 2021
5e09908
Move setup.py to python/ directory
Mandrenkov May 25, 2021
2d08472
Make cleaned directory prefixes consistent
Mandrenkov May 25, 2021
8c5475a
Add 'build' help documentation and clean build/ and dist/
Mandrenkov May 25, 2021
97eed02
Change source directory to parent directory
Mandrenkov May 25, 2021
05b7c08
Import all bindings directly into the 'jet' package
Mandrenkov May 25, 2021
95685fd
Exclude tests from package
Mandrenkov May 25, 2021
71b6622
Remove dependency on 'lark'
Mandrenkov May 25, 2021
01ae9b9
Fix wheel build error for lark
Mandrenkov May 25, 2021
b292e52
Remove deprecated Python bindings build steps
Mandrenkov May 25, 2021
c102416
Update changelog
Mandrenkov May 25, 2021
c86b04a
Adjust PR number
Mandrenkov May 25, 2021
6218b5a
Flatten jet.bindings package and apply formatter
Mandrenkov May 25, 2021
477f548
Change 'lark' requirement to 'lark-parser'
Mandrenkov May 25, 2021
17da911
add circuit class
thisac May 27, 2021
48b82b7
add first gate
thisac May 27, 2021
97a31f7
Merge branch 'main' into xir-interface-gates
thisac May 27, 2021
bf59af7
Add gates (WIP)
thisac May 28, 2021
5ee13e4
Merge branch 'xir-interface-gates' of github.com:XanaduAI/jet into xi…
thisac May 28, 2021
6bc04b8
add gates
thisac May 28, 2021
9c99b6a
Merge branch 'main' into xir-interface-gates
Mandrenkov May 31, 2021
19e7cc0
Add jet/ to formatted directories
Mandrenkov Jun 1, 2021
a1ba8ff
Refactor Gate class
Mandrenkov Jun 1, 2021
5445412
Apply formatter to jet/ directory
Mandrenkov Jun 1, 2021
0fd4c5b
Factor parameter validation to Gate class
Mandrenkov Jun 1, 2021
dc54995
Remove positional arguments after keyword arguments
Mandrenkov Jun 1, 2021
aab1c7c
Merge branch 'main' into xir-interface-gates
Mandrenkov Jun 1, 2021
aac1fab
Move dtype factories to separate module to avoid circular dependency
Mandrenkov Jun 2, 2021
e878297
Add unit tests for the Gate class
Mandrenkov Jun 2, 2021
24f0ba7
Delegate package exports to modules
Mandrenkov Jun 2, 2021
a26448e
Update name of validation parameter
Mandrenkov Jun 2, 2021
08c7975
Add unit tests for H, CNOT, and Pauli gates
Mandrenkov Jun 2, 2021
a99a0b0
Add unit tests for remaining gates
Mandrenkov Jun 2, 2021
0604c8b
Add unit test IDs
Mandrenkov Jun 3, 2021
56c6ac5
Add unit tests for CV Fock gates
Mandrenkov Jun 3, 2021
a389e28
Defer circuit.py to future PR
Mandrenkov Jun 3, 2021
1c7edff
Update changelog
Mandrenkov Jun 3, 2021
1f833d1
Add thewalrus to package requirements and specify numpy versions
Mandrenkov Jun 3, 2021
2722053
Create type aliases for union of template class specializations
Mandrenkov Jun 4, 2021
65d81a8
Convert Gate into an ABC, expose num_wires, and distribute validation
Mandrenkov Jun 4, 2021
4bd800e
Merge branch 'main' into xir-interface-gates
Mandrenkov Jun 4, 2021
db43ea3
Use MockGate instead of CPhaseShift in Gate tests
Mandrenkov Jun 4, 2021
bcf086d
Elaborate docstring comments
Mandrenkov Jun 8, 2021
686c462
Replace conjugate transpose with matrix inverse
Mandrenkov Jun 8, 2021
ccfb710
Replace MockGate data with invertible matrix
Mandrenkov Jun 8, 2021
c68c08b
Move parameter validation to gate constructors
Mandrenkov Jun 8, 2021
886a513
Rename 'gates.py' to 'gate.py'
Mandrenkov Jun 8, 2021
55af49d
Reword @indices.getter docstring
Mandrenkov Jun 8, 2021
4335a1f
Remove leftover *params from Displacement constructor
Mandrenkov Jun 8, 2021
4cf73bc
Import symbols from math and cmath modules explicitly
Mandrenkov Jun 8, 2021
f461881
Rename 'CNOT' gate to 'CX' gate
Mandrenkov Jun 9, 2021
e80f43a
Add aliases for NOT and CNOT gates
Mandrenkov Jun 10, 2021
6708325
Add code span formatting to None
Mandrenkov Jun 11, 2021
c6a782b
Add colon after Exception class in docstring
Mandrenkov Jun 11, 2021
7922fd1
Apply hanging indent to Exception description
Mandrenkov Jun 11, 2021
1b9ac3b
Update wording of num_wires attribute
Mandrenkov Jun 11, 2021
c2d2630
Remove full stop from docstring argument descriptions
Mandrenkov Jun 11, 2021
9534313
Restore full stop and capitalize argument descriptions
Mandrenkov Jun 11, 2021
1b43784
Fix grammar error in transmissivity argument description
Mandrenkov Jun 11, 2021
7552030
Remove tensor ID (and **kwargs) from Gate (sub)class(es)
Mandrenkov Jun 11, 2021
5335954
Test multi-character string indices
Mandrenkov Jun 11, 2021
73777ee
Add descriptions to *args and **kwargs
Mandrenkov Jun 11, 2021
0a4d102
Remove extra space from list
Mandrenkov Jun 11, 2021
0f4647a
Mention Antal and Josh in changelog
Mandrenkov Jun 11, 2021
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
4 changes: 3 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### New features since last release

* Quantum gate models have been added to the `jet` Python package. [(#16)](https://github.com/XanaduAI/jet/pull/16)

* Python bindings are now available for the `TaskBasedCpuContractor` class. [(#19)](https://github.com/XanaduAI/jet/pull/19)

* Python bindings now include a factory method which accepts a `dtype` parameter. [(#18)](https://github.com/XanaduAI/jet/pull/18)
Expand All @@ -22,7 +24,7 @@

* Tensor transposes are now significantly faster when all the dimensions are powers of two. [(#12)](https://github.com/XanaduAI/jet/pull/12)

* Use camel case for type aliases [(#17)](https://github.com/XanaduAI/jet/pull/17)
* Use camel case for type aliases. [(#17)](https://github.com/XanaduAI/jet/pull/17)

* Exceptions are now favoured in place of `std::terminate` with `Exception` being the new base type for all exceptions thrown by Jet. [(#3)](https://github.com/XanaduAI/jet/pull/3)

Expand Down
8 changes: 4 additions & 4 deletions python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ setup: $(.VENV_DIR)/touch
.PHONY: format
format: $(.VENV_DIR)/requirements.txt.touch
ifdef check
$(.VENV_BIN)/black -l 100 --check tests
$(.VENV_BIN)/isort --profile black --check-only tests
$(.VENV_BIN)/black -l 100 --check jet tests
$(.VENV_BIN)/isort --profile black --check-only jet tests
else
$(.VENV_BIN)/black -l 100 tests
$(.VENV_BIN)/isort --profile black tests
$(.VENV_BIN)/black -l 100 jet tests
$(.VENV_BIN)/isort --profile black jet tests
endif


Expand Down
80 changes: 3 additions & 77 deletions python/jet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,9 @@
from typing import Union

import numpy as np

# The existence of a Python binding is proof of its intention to be exposed.
from .bindings import *


def TaskBasedCpuContractor(
*args, **kwargs
) -> Union[TaskBasedCpuContractorC64, TaskBasedCpuContractorC128]:
"""Constructs a task-based CPU contractor with the specified data type. If a
`dtype` keyword argument is not provided, a TaskBasedCpuContractorC128
instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TaskBasedCpuContractorC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TaskBasedCpuContractorC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def Tensor(*args, **kwargs) -> Union[TensorC64, TensorC128]:
"""Constructs a tensor with the specified data type. If a `dtype` keyword
argument is not provided, a TensorC128 instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetwork(*args, **kwargs) -> Union[TensorNetworkC64, TensorNetworkC128]:
"""Constructs a tensor network with the specified data type. If a `dtype`
keyword argument is not provided, a TensorNetworkC128 instance will be
returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetworkFile(*args, **kwargs) -> Union[TensorNetworkFileC64, TensorNetworkFileC128]:
"""Constructs a tensor network file with the specified data type. If a
`dtype` keyword argument is not provided, a TensorNetworkFileC128 instance
will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkFileC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkFileC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetworkSerializer(
*args, **kwargs
) -> Union[TensorNetworkSerializerC64, TensorNetworkSerializerC128]:
"""Constructs a tensor network serializer with the specified data type. If a
`dtype` keyword argument is not provided, a TensorNetworkSerializerC128
instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkSerializerC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkSerializerC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")

# The rest of the modules control their exports using `__all__`.
from .factory import *
from .gate import *

# Grab the current Jet version from the C++ headers.
__version__ = version()
105 changes: 105 additions & 0 deletions python/jet/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from typing import Union
Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np

from .bindings import (
TaskBasedCpuContractorC64,
TaskBasedCpuContractorC128,
TensorC64,
TensorC128,
TensorNetworkC64,
TensorNetworkC128,
TensorNetworkFileC64,
TensorNetworkFileC128,
TensorNetworkSerializerC64,
TensorNetworkSerializerC128,
)

__all__ = [
"TaskBasedCpuContractorType",
"TensorType",
"TensorNetworkType",
"TensorNetworkFileType",
"TensorNetworkSerializerType",
"TaskBasedCpuContractor",
"Tensor",
"TensorNetwork",
"TensorNetworkFile",
"TensorNetworkSerializer",
]

# Type aliases to avoid enumerating class specializations.
TaskBasedCpuContractorType = Union[TaskBasedCpuContractorC64, TaskBasedCpuContractorC128]
TensorType = Union[TensorC64, TensorC128]
TensorNetworkType = Union[TensorNetworkC64, TensorNetworkC128]
TensorNetworkFileType = Union[TensorNetworkFileC64, TensorNetworkFileC128]
TensorNetworkSerializerType = Union[TensorNetworkSerializerC64, TensorNetworkSerializerC128]


def TaskBasedCpuContractor(*args, **kwargs) -> TaskBasedCpuContractorType:
"""Constructs a task-based CPU contractor with the specified data type. If a
Mandrenkov marked this conversation as resolved.
Show resolved Hide resolved
`dtype` keyword argument is not provided, a TaskBasedCpuContractorC128
instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TaskBasedCpuContractorC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TaskBasedCpuContractorC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def Tensor(*args, **kwargs) -> TensorType:
"""Constructs a tensor with the specified data type. If a `dtype` keyword
argument is not provided, a TensorC128 instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetwork(*args, **kwargs) -> TensorNetworkType:
"""Constructs a tensor network with the specified data type. If a `dtype`
keyword argument is not provided, a TensorNetworkC128 instance will be
returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetworkFile(*args, **kwargs) -> TensorNetworkFileType:
"""Constructs a tensor network file with the specified data type. If a
`dtype` keyword argument is not provided, a TensorNetworkFileC128 instance
will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkFileC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkFileC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")


def TensorNetworkSerializer(*args, **kwargs) -> TensorNetworkSerializerType:
"""Constructs a tensor network serializer with the specified data type. If a
`dtype` keyword argument is not provided, a TensorNetworkSerializerC128
instance will be returned.
"""
dtype = kwargs.pop("dtype", "complex128")
if np.dtype(dtype) == np.complex64:
return TensorNetworkSerializerC64(*args, **kwargs)
elif np.dtype(dtype) == np.complex128:
return TensorNetworkSerializerC128(*args, **kwargs)
else:
raise TypeError(f"Data type '{dtype}' is not supported.")
Loading