Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust
Browse files Browse the repository at this point in the history
dominikandreasseitz committed Oct 24, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c50dc58 commit 090c459
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion qadence/backends/pyqtorch/backend.py
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ def run(
else:
batch_size = max([len(tensor) for tensor in param_values.values()])
state = circuit.native.init_state(batch_size=batch_size)
state = circuit.native(state, param_values)
state = circuit.native.run(state, param_values)

# make sure that the batch dimension is the first one, as standard
# for PyTorch, and not the last one as done in PyQ
18 changes: 11 additions & 7 deletions qadence/backends/pyqtorch/convert_ops.py
Original file line number Diff line number Diff line change
@@ -76,15 +76,19 @@ def convert_block(
# TODO: use the hevo_algo configuration here to switch between different algorithms
# for executing the Hamiltonian evolution
operation=pyq.HamiltonianEvolution(
block.qubit_support,
list(block.qubit_support),
n_qubits,
n_steps=config.n_steps_hevo,
),
block=block,
config=config,
)
else:
elif block.n_supports == 1:
op = getattr(pyq, block.name)(block.qubit_support[0], config.get_param_name(block)[0])
else:
op = getattr(pyq, block.name)(
block.qubit_support[0], block.qubit_support[1], config.get_param_name(block)[0]
)
return [op]
elif isinstance(block, MatrixBlock):
return [PyQMatrixBlock(block, n_qubits, config)]
@@ -151,13 +155,13 @@ def forward(
self, state: torch.Tensor, values: dict[str, torch.Tensor] | None = None
) -> torch.Tensor:
batch_size = state.size(-1)
return self.apply(self.matrices(values, batch_size), state)
return self.apply(self.unitary(values, batch_size), state)

def apply(self, matrices: torch.Tensor, state: torch.Tensor) -> torch.Tensor:
batch_size = state.size(-1)
return _apply_batch_gate(state, matrices, self.qubits, self.n_qubits, batch_size)

def matrices(self, values: dict[str, torch.Tensor] | None, batch_size: int) -> torch.Tensor:
def unitary(self, values: dict[str, torch.Tensor] | None, batch_size: int) -> torch.Tensor:
perm = (2, 0, 1) # We permute the dims since torch.bmm expects the batch_dim at 0.

def _expand_mat(m: torch.Tensor) -> torch.Tensor:
@@ -172,7 +176,7 @@ def _expand_mat(m: torch.Tensor) -> torch.Tensor:
# We reverse the list of tensors here since matmul is not commutative.
return torch.permute(
reduce(
torch.bmm, (_expand_mat(op.matrices(values)) for op in reversed(self.operations))
torch.bmm, (_expand_mat(op.unitary(values)) for op in reversed(self.operations))
),
tuple(
torch.argsort(torch.tensor(perm))
@@ -364,9 +368,9 @@ def _forward(state: torch.Tensor, values: dict[str, torch.Tensor]) -> torch.Tens

self._forward = _forward

def matrices(self, values: dict[str, torch.Tensor]) -> torch.Tensor:
def unitary(self, values: dict[str, torch.Tensor]) -> torch.Tensor:
thetas = values[self.param_name]
return (thetas * self.operation.matrices()).unsqueeze(2)
return (thetas * self.operation.unitary(values)).unsqueeze(2)

def forward(self, state: torch.Tensor, values: dict[str, torch.Tensor]) -> torch.Tensor:
return self._forward(state, values)
6 changes: 3 additions & 3 deletions tests/backends/pyq/test_quantum_pyq.py
Original file line number Diff line number Diff line change
@@ -288,11 +288,11 @@ def test_run_with_parametric_single_qubit_gates(


def test_ugate_pure_pyqtorch() -> None:
import pyqtorch.modules as pyqtorch
import pyqtorch as pyq

thetas = torch.rand(3)
state = pyqtorch.zero_state(n_qubits=1, dtype=torch.complex128)
pyqtorch_u = pyqtorch.U(qubits=[0], n_qubits=1)
state = pyq.zero_state(n_qubits=1, dtype=torch.complex128)
pyqtorch_u = pyq.U(qubits=[0], n_qubits=1)
Qadence_u = U(0, phi=thetas[0], theta=thetas[1], omega=thetas[2])
circ = QuantumCircuit(1, Qadence_u)
backend = Backend()

0 comments on commit 090c459

Please sign in to comment.