Skip to content

Commit 8ae1e9a

Browse files
authored
Merge pull request #105 from BQSKit/1.0.3
1.0.3
2 parents 4e62a6c + 76db2ca commit 8ae1e9a

File tree

16 files changed

+342
-35153
lines changed

16 files changed

+342
-35153
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ repos:
4141
- --max-line-length=80
4242
- --ignore=E731
4343
- repo: https://github.com/asottile/pyupgrade
44-
rev: v2.38.2
44+
rev: v3.1.0
4545
hooks:
4646
- id: pyupgrade
4747
args:
4848
- --py38-plus
4949
- repo: https://github.com/asottile/reorder_python_imports
50-
rev: v3.8.3
50+
rev: v3.9.0
5151
hooks:
5252
- id: reorder-python-imports
5353
args:
@@ -61,17 +61,17 @@ repos:
6161
args:
6262
- --py36-plus
6363
- repo: https://github.com/asottile/setup-cfg-fmt
64-
rev: v2.0.0
64+
rev: v2.2.0
6565
hooks:
6666
- id: setup-cfg-fmt
6767
- repo: https://github.com/PyCQA/autoflake
68-
rev: v1.6.1
68+
rev: v1.7.7
6969
hooks:
7070
- id: autoflake
7171
args:
7272
- --in-place
7373
- repo: https://github.com/pre-commit/mirrors-mypy
74-
rev: v0.971
74+
rev: v0.982
7575
hooks:
7676
- id: mypy
7777
exclude: tests/qis/test_pauli.py

bqskit/ir/circuit.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from bqskit.ir.region import CircuitRegion
4545
from bqskit.ir.region import CircuitRegionLike
4646
from bqskit.qis.graph import CouplingGraph
47-
from bqskit.qis.permutation import PermutationMatrix
4847
from bqskit.qis.state.state import StateLike
4948
from bqskit.qis.state.state import StateVector
5049
from bqskit.qis.state.statemap import StateVectorMap
@@ -2578,29 +2577,19 @@ def get_unitary_and_grad(
25782577
# Calculate gradient
25792578
left = UnitaryBuilder(self.num_qudits, self.radixes)
25802579
right = UnitaryBuilder(self.num_qudits, self.radixes)
2581-
full_gards = []
2580+
full_grads = []
25822581

25832582
for M, loc in zip(matrices, locations):
25842583
right.apply_right(M, loc)
25852584

25862585
for M, dM, loc in zip(matrices, grads, locations):
2587-
perm = PermutationMatrix.from_qubit_location(self.num_qudits, loc)
2588-
permT = perm.T
2589-
iden = np.identity(2 ** (self.num_qudits - len(loc)))
2590-
25912586
right.apply_left(M, loc, inverse=True)
25922587
right_utry = right.get_unitary()
2593-
left_utry = left.get_unitary()
25942588
for grad in dM:
2595-
# TODO: use tensor contractions here instead of mm
2596-
# Should work fine with non unitary gradients
2597-
# TODO: Fix for non qubits
2598-
full_grad = np.kron(grad, iden)
2599-
full_grad = permT @ full_grad @ perm
2600-
full_gards.append(right_utry @ full_grad @ left_utry)
2589+
full_grads.append(right_utry @ left.eval_apply_right(grad, loc))
26012590
left.apply_right(M, loc)
26022591

2603-
return left.get_unitary(), np.array(full_gards)
2592+
return left.get_unitary(), np.array(full_grads)
26042593

26052594
def instantiate(
26062595
self,

bqskit/ir/gate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
from typing import Callable
10+
from typing import ClassVar
1011
from typing import TYPE_CHECKING
1112

1213
from bqskit.ir.location import CircuitLocation
@@ -51,8 +52,12 @@ def get_qasm(self, params: RealVector, location: CircuitLocation) -> str:
5152
'], q['.join([str(q) for q in location]),
5253
).replace('()', '')
5354

54-
with_frozen_params: Callable[[Gate, dict[int, float]], FrozenParameterGate]
55-
with_all_frozen_params: Callable[[Gate, list[float]], FrozenParameterGate]
55+
with_frozen_params: ClassVar[
56+
Callable[[Gate, dict[int, float]], FrozenParameterGate]
57+
]
58+
with_all_frozen_params: ClassVar[
59+
Callable[[Gate, list[float]], FrozenParameterGate]
60+
]
5661

5762
def __repr__(self) -> str:
5863
return self.name

bqskit/ir/gates/composed/controlled.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from bqskit.qis.unitary.differentiable import DifferentiableUnitary
1111
from bqskit.qis.unitary.unitary import RealVector
1212
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
13+
from bqskit.utils.docs import building_docs
1314
from bqskit.utils.typing import is_integer
1415

1516

@@ -69,7 +70,7 @@ def __init__(
6970
self.left = np.kron((self.Ic - self.OneProj), self.It)
7071

7172
# If input is a constant gate, we can cache the unitary.
72-
if self.num_params == 0:
73+
if self.num_params == 0 and not building_docs():
7374
U = self.gate.get_unitary()
7475
right = np.kron(self.OneProj, U)
7576
self.utry = UnitaryMatrix(self.left + right, self.radixes)

bqskit/ir/gates/composed/daggergate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from bqskit.qis.unitary.optimizable import LocallyOptimizableUnitary
1111
from bqskit.qis.unitary.unitary import RealVector
1212
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
13+
from bqskit.utils.docs import building_docs
1314

1415

1516
class DaggerGate(
@@ -45,7 +46,7 @@ def __init__(self, gate: Gate) -> None:
4546
self._radixes = gate.radixes
4647

4748
# If input is a constant gate, we can cache the unitary.
48-
if self.num_params == 0:
49+
if self.num_params == 0 and not building_docs():
4950
self.utry = gate.get_unitary().dagger
5051

5152
def get_unitary(self, params: RealVector = []) -> UnitaryMatrix:

bqskit/ir/gates/composed/tagged.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from bqskit.qis.unitary.optimizable import LocallyOptimizableUnitary
1313
from bqskit.qis.unitary.unitary import RealVector
1414
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
15+
from bqskit.utils.docs import building_docs
1516

1617

1718
class TaggedGate(
@@ -46,7 +47,7 @@ def __init__(self, gate: Gate, tag: Any) -> None:
4647
self._radixes = gate.radixes
4748

4849
# If input is a constant gate, we can cache the unitary.
49-
if self.num_params == 0:
50+
if self.num_params == 0 and not building_docs():
5051
self.utry = gate.get_unitary()
5152

5253
def get_unitary(self, params: RealVector = []) -> UnitaryMatrix:

0 commit comments

Comments
 (0)