Skip to content

Commit

Permalink
Merge pull request #105 from BQSKit/1.0.3
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
edyounis authored Oct 28, 2022
2 parents 4e62a6c + 76db2ca commit 8ae1e9a
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 35,153 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ repos:
- --max-line-length=80
- --ignore=E731
- repo: https://github.com/asottile/pyupgrade
rev: v2.38.2
rev: v3.1.0
hooks:
- id: pyupgrade
args:
- --py38-plus
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.8.3
rev: v3.9.0
hooks:
- id: reorder-python-imports
args:
Expand All @@ -61,17 +61,17 @@ repos:
args:
- --py36-plus
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.0.0
rev: v2.2.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/autoflake
rev: v1.6.1
rev: v1.7.7
hooks:
- id: autoflake
args:
- --in-place
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
rev: v0.982
hooks:
- id: mypy
exclude: tests/qis/test_pauli.py
Expand Down
17 changes: 3 additions & 14 deletions bqskit/ir/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from bqskit.ir.region import CircuitRegion
from bqskit.ir.region import CircuitRegionLike
from bqskit.qis.graph import CouplingGraph
from bqskit.qis.permutation import PermutationMatrix
from bqskit.qis.state.state import StateLike
from bqskit.qis.state.state import StateVector
from bqskit.qis.state.statemap import StateVectorMap
Expand Down Expand Up @@ -2578,29 +2577,19 @@ def get_unitary_and_grad(
# Calculate gradient
left = UnitaryBuilder(self.num_qudits, self.radixes)
right = UnitaryBuilder(self.num_qudits, self.radixes)
full_gards = []
full_grads = []

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

for M, dM, loc in zip(matrices, grads, locations):
perm = PermutationMatrix.from_qubit_location(self.num_qudits, loc)
permT = perm.T
iden = np.identity(2 ** (self.num_qudits - len(loc)))

right.apply_left(M, loc, inverse=True)
right_utry = right.get_unitary()
left_utry = left.get_unitary()
for grad in dM:
# TODO: use tensor contractions here instead of mm
# Should work fine with non unitary gradients
# TODO: Fix for non qubits
full_grad = np.kron(grad, iden)
full_grad = permT @ full_grad @ perm
full_gards.append(right_utry @ full_grad @ left_utry)
full_grads.append(right_utry @ left.eval_apply_right(grad, loc))
left.apply_right(M, loc)

return left.get_unitary(), np.array(full_gards)
return left.get_unitary(), np.array(full_grads)

def instantiate(
self,
Expand Down
9 changes: 7 additions & 2 deletions bqskit/ir/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

from typing import Callable
from typing import ClassVar
from typing import TYPE_CHECKING

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

with_frozen_params: Callable[[Gate, dict[int, float]], FrozenParameterGate]
with_all_frozen_params: Callable[[Gate, list[float]], FrozenParameterGate]
with_frozen_params: ClassVar[
Callable[[Gate, dict[int, float]], FrozenParameterGate]
]
with_all_frozen_params: ClassVar[
Callable[[Gate, list[float]], FrozenParameterGate]
]

def __repr__(self) -> str:
return self.name
3 changes: 2 additions & 1 deletion bqskit/ir/gates/composed/controlled.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bqskit.qis.unitary.differentiable import DifferentiableUnitary
from bqskit.qis.unitary.unitary import RealVector
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
from bqskit.utils.docs import building_docs
from bqskit.utils.typing import is_integer


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

# If input is a constant gate, we can cache the unitary.
if self.num_params == 0:
if self.num_params == 0 and not building_docs():
U = self.gate.get_unitary()
right = np.kron(self.OneProj, U)
self.utry = UnitaryMatrix(self.left + right, self.radixes)
Expand Down
3 changes: 2 additions & 1 deletion bqskit/ir/gates/composed/daggergate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bqskit.qis.unitary.optimizable import LocallyOptimizableUnitary
from bqskit.qis.unitary.unitary import RealVector
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
from bqskit.utils.docs import building_docs


class DaggerGate(
Expand Down Expand Up @@ -45,7 +46,7 @@ def __init__(self, gate: Gate) -> None:
self._radixes = gate.radixes

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

def get_unitary(self, params: RealVector = []) -> UnitaryMatrix:
Expand Down
3 changes: 2 additions & 1 deletion bqskit/ir/gates/composed/tagged.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from bqskit.qis.unitary.optimizable import LocallyOptimizableUnitary
from bqskit.qis.unitary.unitary import RealVector
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
from bqskit.utils.docs import building_docs


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

# If input is a constant gate, we can cache the unitary.
if self.num_params == 0:
if self.num_params == 0 and not building_docs():
self.utry = gate.get_unitary()

def get_unitary(self, params: RealVector = []) -> UnitaryMatrix:
Expand Down
Loading

0 comments on commit 8ae1e9a

Please sign in to comment.