Skip to content

Commit 6a370b9

Browse files
authored
Qiskit gates: Support GlobalPhaseGate (#380)
* update sampler docstring * Qiskit gates: Support GlobalPhaseGate
1 parent 5d693cb commit 6a370b9

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

python/ffsim/qiskit/sampler.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ def __init__(
6060
6161
2. Use Qiskit gates. The circuit should begin with some ``X`` gates. Next, a
6262
number of unitary gates are applied. The following unitary gates are supported:
63-
[``CPhaseGate``, ``PhaseGate``, ``RZGate``, ``RZZGate``, ``SwapGate``,
64-
``XXPlusYYGate``].
63+
[``CPhaseGate``, ``CZGate``, ``GlobalPhaseGate``, ``iSwapGate``, ``PhaseGate``,
64+
``RZGate``, ``RZZGate``, ``SGate``, ``SdgGate``, ``SwapGate``, ``TGate``,
65+
``TdgGate``, ``XXPlusYYGate``, ``ZGate``].
6566
Finally, measurement gates must only occur at the end of the circuit.
6667
6768
When simulating spinful circuits constructed from Qiskit gates, you should
6869
pass the `norb` and `nelec` arguments to the FfsimSampler initialization.
6970
Otherwise, a spinless simulation will be performed, which is less efficient.
7071
72+
Currently, spinless circuits are limited to 64 qubits, and spinful circuits are
73+
limited to 128 qubits.
74+
7175
Args:
7276
default_shots: The default shots to use if not specified during run.
7377
norb: The number of spatial orbitals.

python/ffsim/qiskit/sim.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Barrier,
2323
CPhaseGate,
2424
CZGate,
25+
GlobalPhaseGate,
2526
Measure,
2627
PhaseGate,
2728
RZGate,
@@ -183,7 +184,7 @@ def _evolve_state_vector_spinless(
183184
) -> states.StateVector:
184185
op = instruction.operation
185186
qubit_indices = [circuit.find_bit(qubit).index for qubit in instruction.qubits]
186-
consecutive_sorted = qubit_indices == list(
187+
consecutive_sorted = not qubit_indices or qubit_indices == list(
187188
range(min(qubit_indices), max(qubit_indices) + 1)
188189
)
189190
vec = state_vector.vec
@@ -337,6 +338,11 @@ def _evolve_state_vector_spinless(
337338
)
338339
return states.StateVector(vec=vec, norb=norb, nelec=nelec)
339340

341+
if isinstance(op, GlobalPhaseGate):
342+
(phase,) = op.params
343+
vec *= cmath.rect(1, phase)
344+
return states.StateVector(vec=vec, norb=norb, nelec=nelec)
345+
340346
raise ValueError(f"Unsupported gate for spinless circuit: {op}.")
341347

342348

@@ -347,7 +353,7 @@ def _evolve_state_vector_spinful(
347353
) -> states.StateVector:
348354
op = instruction.operation
349355
qubit_indices = [circuit.find_bit(qubit).index for qubit in instruction.qubits]
350-
consecutive_sorted = qubit_indices == list(
356+
consecutive_sorted = not qubit_indices or qubit_indices == list(
351357
range(min(qubit_indices), max(qubit_indices) + 1)
352358
)
353359
vec = state_vector.vec
@@ -604,6 +610,11 @@ def _evolve_state_vector_spinful(
604610
)
605611
return states.StateVector(vec=vec, norb=norb, nelec=nelec)
606612

613+
if isinstance(op, GlobalPhaseGate):
614+
(phase,) = op.params
615+
vec *= cmath.rect(1, phase)
616+
return states.StateVector(vec=vec, norb=norb, nelec=nelec)
617+
607618
raise ValueError(f"Unsupported gate for spinful circuit: {op}.")
608619

609620

tests/python/qiskit/sim_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from qiskit.circuit.library import (
2222
CPhaseGate,
2323
CZGate,
24+
GlobalPhaseGate,
2425
PhaseGate,
2526
RZGate,
2627
RZZGate,
@@ -204,6 +205,7 @@ def test_qiskit_gates_spinful(norb: int, nelec: tuple[int, int]):
204205
)
205206
circuit.append(SwapGate(), [qubits[i], qubits[j]])
206207
circuit.append(SwapGate(), [qubits[norb + i], qubits[norb + j]])
208+
circuit.append(GlobalPhaseGate(rng.uniform(-10, 10)))
207209

208210
# Compute state vector using ffsim
209211
ffsim_vec = ffsim.qiskit.final_state_vector(circuit, norb=norb, nelec=nelec)
@@ -258,6 +260,7 @@ def test_qiskit_gates_spinless(norb: int, nocc: int):
258260
)
259261
for i, j in pairs:
260262
circuit.append(SwapGate(), [qubits[i], qubits[j]])
263+
circuit.append(GlobalPhaseGate(rng.uniform(-10, 10)))
261264

262265
# Compute state vector using ffsim
263266
ffsim_vec = ffsim.qiskit.final_state_vector(circuit, norb=norb, nelec=nocc)

0 commit comments

Comments
 (0)