This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
229 lines (196 loc) · 8.57 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright 2018-2023 Xanadu Quantum Technologies Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import platform
import subprocess
import shutil
import sys
from pathlib import Path
from setuptools import setup, Extension, find_namespace_packages
from setuptools.command.build_ext import build_ext
default_backend = "lightning_qubit"
supported_backends = {"lightning_kokkos", "lightning_qubit"}
supported_backends.update({sb.replace("_", ".") for sb in supported_backends})
def get_backend():
"""Return backend.
The backend is ``lightning_qubit`` by default.
Allowed values are: "lightning_kokkos", "lightning_qubit".
A dot can also be used instead of an underscore.
If the environment variable ``PL_BACKEND`` is defined, its value is used.
Otherwise, if the environment variable ``CMAKE_ARGS`` is defined and it
contains the CMake option ``PL_BACKEND``, its value is used.
Dots are replaced by underscores upon exiting.
"""
backend = None
if "PL_BACKEND" in os.environ:
backend = os.environ.get("PL_BACKEND", default_backend)
backend = backend.replace(".", "_")
if "CMAKE_ARGS" in os.environ:
cmake_args = os.environ["CMAKE_ARGS"].split(" ")
arg = [x for x in cmake_args if "PL_BACKEND" in x]
if not arg and backend is not None:
cmake_backend = backend
else:
cmake_backend = arg[0].split("=")[1].replace(".", "_") if arg else default_backend
if backend is not None and backend != cmake_backend:
raise ValueError(
f"Backends {backend} and {cmake_backend} specified by PL_BACKEND and CMAKE_ARGS respectively do not match."
)
backend = cmake_backend
if backend is None:
backend = default_backend
if backend not in supported_backends:
raise ValueError(f"Invalid backend {backend}.")
return backend
backend = get_backend()
device_name = backend.replace("_", ".")
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = Path(sourcedir).absolute()
class CMakeBuild(build_ext):
"""
This class is built upon https://github.com/diegoferigo/cmake-build-extension/blob/master/src/cmake_build_extension/build_extension.py and https://github.com/pybind/cmake_example/blob/master/setup.py
"""
user_options = build_ext.user_options + [("define=", "D", "Define variables for CMake")]
def initialize_options(self):
super().initialize_options()
self.define = None
self.verbosity = ""
def finalize_options(self):
# Parse the custom CMake options and store them in a new attribute
defines = [] if self.define is None else self.define.split(";")
self.cmake_defines = [f"-D{define}" for define in defines]
if self.verbosity != "":
self.verbosity = "--verbose"
super().finalize_options()
def build_extension(self, ext: CMakeExtension):
extdir = str(Path(self.get_ext_fullpath(ext.name)).parent.absolute())
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
build_type = "Debug" if debug else "RelWithDebInfo"
ninja_path = str(shutil.which("ninja"))
build_args = ["--config", "Debug"] if debug else ["--config", "RelWithDebInfo"]
configure_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
f"-DCMAKE_BUILD_TYPE={build_type}", # not used on MSVC, but no harm
"-DENABLE_WARNINGS=OFF", # Ignore warnings
]
configure_args += (
[f"-DPYTHON_EXECUTABLE={sys.executable}"]
if platform.system() == "Linux"
else [f"-DPython_EXECUTABLE={sys.executable}"]
)
if platform.system() == "Windows":
# As Ninja does not support long path for windows yet:
# (https://github.com/ninja-build/ninja/pull/2056)
configure_args += [
"-T clangcl",
]
elif ninja_path:
configure_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM={ninja_path}",
]
configure_args += [f"-DPL_BACKEND={backend}"]
configure_args += self.cmake_defines
# Add more platform dependent options
if platform.system() == "Darwin":
clang_path = Path(shutil.which("clang++")).parent.parent
configure_args += [
f"-DCMAKE_CXX_COMPILER={clang_path}/bin/clang++",
f"-DCMAKE_LINKER={clang_path}/bin/lld",
]
if shutil.which("brew"):
libomp_path = subprocess.run(
"brew --prefix libomp".split(" "),
check=False,
capture_output=True,
text=True,
).stdout.strip()
if not Path(libomp_path).exists():
libomp_path = ""
configure_args += (
[f"-DOpenMP_ROOT={libomp_path}/"] if libomp_path else ["-DENABLE_OPENMP=OFF"]
)
elif platform.system() == "Windows":
configure_args += ["-DENABLE_OPENMP=OFF", "-DENABLE_BLAS=OFF"]
elif platform.system() not in ["Linux"]:
raise RuntimeError(f"Unsupported '{platform.system()}' platform")
if not Path(self.build_temp).exists():
os.makedirs(self.build_temp)
if "CMAKE_ARGS" in os.environ:
configure_args += os.environ["CMAKE_ARGS"].split(" ")
subprocess.check_call(
["cmake", str(ext.sourcedir)] + configure_args,
cwd=self.build_temp,
env=os.environ,
)
subprocess.check_call(
["cmake", "--build", ".", "--verbose"] + build_args,
cwd=self.build_temp,
env=os.environ,
)
with open(os.path.join("pennylane_lightning", "core", "_version.py"), encoding="utf-8") as f:
version = f.readlines()[-1].split()[-1].strip("\"'")
with open("README.md", encoding="utf-8") as f:
readme = f.read()
requirements = [
"pennylane @ git+https://github.com/PennyLaneAI/pennylane.git@feature/lightning_ready#egg=pennylane",
]
suffix = backend.replace("lightning_", "")
suffix = suffix[0].upper() + suffix[1:]
pennylane_plugins = [device_name + " = pennylane_lightning." + backend + ":Lightning" + suffix]
info = {
"name": f"PennyLane_Lightning_{suffix}",
"version": version,
"maintainer": "Xanadu Inc.",
"maintainer_email": "[email protected]",
"url": "https://github.com/XanaduAI/pennylane-lightning",
"license": "Apache License 2.0",
"packages": find_namespace_packages(include=['pennylane_lightning.core',
'pennylane_lightning.'+backend]),
"package_data": {
'pennylane_lightning.core': [
os.path.join("src", "*"),
os.path.join("src", "**", "*"),
]
},
"include_package_data": True,
"entry_points": {"pennylane.plugins": pennylane_plugins},
"description": "PennyLane-Lightning plugin",
"long_description": readme,
"long_description_content_type": "text/markdown",
"install_requires": requirements,
"ext_modules": []
if os.environ.get("SKIP_COMPILATION", False)
else [CMakeExtension(f"{backend}_ops")],
"cmdclass": {"build_ext": CMakeBuild},
"ext_package": "pennylane_lightning",
}
classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: Apache Software License",
"Natural Language :: English",
"Operating System :: POSIX",
"Operating System :: MacOS :: MacOS X",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Scientific/Engineering :: Physics",
]
setup(classifiers=classifiers, **(info))