diff --git a/qadence/backends/pyqtorch/backend.py b/qadence/backends/pyqtorch/backend.py index 497e60774..362c4aaa7 100644 --- a/qadence/backends/pyqtorch/backend.py +++ b/qadence/backends/pyqtorch/backend.py @@ -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 diff --git a/qadence/backends/pyqtorch/convert_ops.py b/qadence/backends/pyqtorch/convert_ops.py index fe3f07c16..62dc1aad0 100644 --- a/qadence/backends/pyqtorch/convert_ops.py +++ b/qadence/backends/pyqtorch/convert_ops.py @@ -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) diff --git a/tests/backends/pyq/test_quantum_pyq.py b/tests/backends/pyq/test_quantum_pyq.py index 8e1dbf963..ebc2e0cc4 100644 --- a/tests/backends/pyq/test_quantum_pyq.py +++ b/tests/backends/pyq/test_quantum_pyq.py @@ -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()