Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions python/ffsim/qiskit/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
DiagonalGate,
GlobalPhaseGate,
Measure,
PermutationGate,
PhaseGate,
RZGate,
RZZGate,
Expand Down Expand Up @@ -379,6 +380,19 @@ def _evolve_state_vector_spinless(
vec = _apply_iswap(vec, (i, j), norb=norb, nelec=nelec, copy=False)
return states.StateVector(vec=vec, norb=norb, nelec=nelec)

if isinstance(op, PermutationGate):
perm = list(op.pattern)
qs = qubit_indices.copy()
for k in range(len(perm)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be improved by factoring out a generator function that yields the swap pairs of the permutation decomposition. Then this can look something like

for i, j in _decompose_permutation(perm):
    vec = _apply_swap(vec, (qs[i], qs[j]), ... )

while perm[k] != k:
j = perm[k]
vec = _apply_swap(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will introduce many "swap defect" sign fixing operations. I wonder if we can use FSWAPS instead of SWAPS, and then only fix the signs a single time at the end. Or maybe it is not straightforward ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out!
I have to switch the implementation to use FSWAPs for each transposition so the $−1$ on $|11\rangle$ is handled locally. So it can removes those repeated sign corrections.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's possible to use FSWAPs instead of SWAPs, then the entire permutation can actually be performed as a single orbital rotation.

vec, (qs[k], qs[j]), norb=norb, nelec=nelec, copy=False
)
perm[k], perm[j] = perm[j], perm[k]
qs[k], qs[j] = qs[j], qs[k]
return states.StateVector(vec=vec, norb=norb, nelec=nelec)

if isinstance(op, XXPlusYYGate):
i, j = qubit_indices
theta, beta = op.params
Expand Down Expand Up @@ -715,6 +729,30 @@ def _evolve_state_vector_spinful(
)
return states.StateVector(vec=vec, norb=norb, nelec=nelec)

if isinstance(op, PermutationGate):
perm = list(op.pattern)
qs = qubit_indices.copy()
for k in range(len(perm)):
while perm[k] != k:
j = perm[k]
if (qs[k] < norb) != (qs[j] < norb):
raise ValueError(
f"Gate of type '{op.__class__.__name__}' must be applied on "
"orbitals of the same spin."
)
spin = Spin.ALPHA if qs[k] < norb else Spin.BETA
vec = _apply_swap(
vec,
(qs[k] % norb, qs[j] % norb),
norb=norb,
nelec=nelec,
spin=spin,
copy=False,
)
perm[k], perm[j] = perm[j], perm[k]
qs[k], qs[j] = qs[j], qs[k]
return states.StateVector(vec=vec, norb=norb, nelec=nelec)

if isinstance(op, XXPlusYYGate):
i, j = qubit_indices
if (i < norb) != (j < norb):
Expand Down
4 changes: 4 additions & 0 deletions tests/python/qiskit/sim_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CZGate,
DiagonalGate,
GlobalPhaseGate,
PermutationGate,
PhaseGate,
RZGate,
RZZGate,
Expand Down Expand Up @@ -230,6 +231,8 @@ def test_qiskit_gates_spinful(norb: int, nelec: tuple[int, int]):
circuit.append(DiagonalGate(diag), [qubits[i] for i in chosen])
diag = np.exp(1j * rng.uniform(-np.pi, np.pi, size=1 << 2 * norb))
circuit.append(DiagonalGate(diag), qubits)
circuit.append(PermutationGate(list(rng.permutation(norb))), qubits[:norb])
circuit.append(PermutationGate(list(rng.permutation(norb))), qubits[norb:])
circuit.append(GlobalPhaseGate(rng.uniform(-10, 10)))

# Compute state vector using ffsim
Expand Down Expand Up @@ -304,6 +307,7 @@ def test_qiskit_gates_spinless(norb: int, nocc: int):
circuit.append(DiagonalGate(diag), [qubits[i] for i in chosen])
diag = np.exp(1j * rng.uniform(-np.pi, np.pi, size=1 << norb))
circuit.append(DiagonalGate(diag), qubits)
circuit.append(PermutationGate(list(rng.permutation(norb))), qubits)
circuit.append(GlobalPhaseGate(rng.uniform(-10, 10)))

# Compute state vector using ffsim
Expand Down
Loading