Skip to content

Commit

Permalink
fix controlled ops with dm
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles MOUSSA committed Dec 10, 2024
1 parent 304cdf4 commit aea2d33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion horqrux/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def apply_operator(
return jnp.moveaxis(a=state, source=new_state_dims, destination=state_dims)

# Apply operator to density matrix: ρ' = O ρ O†
support_perm = target + tuple(set(tuple(range(state.ndim // 2))) - set(target))
support_perm = state_dims + tuple(set(tuple(range(state.ndim // 2))) - set(state_dims))
state = permute_basis(state, support_perm, False)
state = jnp.tensordot(a=operator_reshaped, b=state, axes=(op_out_dims, new_state_dims))

Expand Down
20 changes: 20 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def test_controlled_primitive(gate_fn: Callable) -> None:
apply_operator(state, gate.dagger(), gate.target[0], gate.control[0]), orig_state
)

# test density matrix is similar to pure state
dm = apply_operator(
density_mat(orig_state),
gate.unitary(),
gate.target[0],
gate.control[0],
is_state_densitymat=True,
)
assert jnp.allclose(dm, density_mat(state))


@pytest.mark.parametrize("gate_fn", PARAMETRIC_GATES)
def test_parametric(gate_fn: Callable) -> None:
Expand Down Expand Up @@ -89,6 +99,16 @@ def test_controlled_parametric(gate_fn: Callable) -> None:
apply_operator(state, gate.dagger(values), gate.target[0], gate.control[0]), orig_state
)

# test density matrix is similar to pure state
dm = apply_operator(
density_mat(orig_state),
gate.unitary(values),
gate.target[0],
gate.control[0],
is_state_densitymat=True,
)
assert jnp.allclose(dm, density_mat(state))


@pytest.mark.parametrize(
["bitstring", "expected_state"],
Expand Down

0 comments on commit aea2d33

Please sign in to comment.