Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffsim sampler: return bit array directly #324

Merged
merged 2 commits into from
Oct 9, 2024
Merged
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
15 changes: 8 additions & 7 deletions python/ffsim/qiskit/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,24 @@ def _run_pub(self, pub: SamplerPub) -> SamplerPubResult:
for item in meas_info
}
for index, bound_circuit in np.ndenumerate(bound_circuits):
final_state = final_state_vector(bound_circuit)
if qargs:
final_state = final_state_vector(bound_circuit)
norb, nelec = final_state.norb, final_state.nelec
if isinstance(nelec, int):
orbs = qargs
else:
orbs_a = [q for q in qargs if q < norb]
orbs_b = [q % norb for q in qargs if q >= norb]
orbs = (orbs_a, orbs_b)
samples = states.sample_state_vector(
final_state, orbs=orbs, shots=pub.shots, seed=self._rng
samples_array = states.sample_state_vector(
final_state,
orbs=orbs,
shots=pub.shots,
bitstring_type=states.BitstringType.BIT_ARRAY,
seed=self._rng,
)
else:
samples = [""] * pub.shots
samples_array = np.array(
[np.fromiter(sample, dtype=np.uint8) for sample in samples]
)
samples_array = np.empty((pub.shots, 0), dtype=bool)
for item in meas_info:
ary = _samples_to_packed_array(
samples_array, item.num_bits, item.qreg_indices
Expand Down
6 changes: 3 additions & 3 deletions python/ffsim/states/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def sample_state_vector(
concatenate: bool = True,
bitstring_type: BitstringType = BitstringType.STRING,
seed: np.random.Generator | int | None = None,
) -> list[str] | tuple[list[str], list[str]]:
):
"""Sample bitstrings from a state vector.

Args:
Expand Down Expand Up @@ -488,7 +488,7 @@ def _sample_state_vector_spinless(
shots: int,
bitstring_type: BitstringType,
seed: np.random.Generator | int | None,
) -> list[str]:
):
if orbs is None:
orbs = range(norb)
rng = np.random.default_rng(seed)
Expand All @@ -510,7 +510,7 @@ def _sample_state_vector_spinful(
concatenate: bool = True,
bitstring_type: BitstringType,
seed: np.random.Generator | int | None,
) -> list[str] | tuple[list[str], list[str]]:
):
if orbs is None:
orbs = range(norb), range(norb)
rng = np.random.default_rng(seed)
Expand Down
14 changes: 14 additions & 0 deletions tests/python/qiskit/sampler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,17 @@ def test_reproducible_with_seed():
counts_2 = pub_result.data.meas.get_counts()

assert counts_1 == counts_2


def test_edge_cases():
"""Test edge cases."""
with pytest.raises(
ValueError, match="Circuit must contain at least one instruction."
):
qubits = QuantumRegister(1, name="q")
circuit = QuantumCircuit(qubits)
circuit.measure_all()
sampler = ffsim.qiskit.FfsimSampler(default_shots=1)
pub = (circuit,)
job = sampler.run([pub])
_ = job.result()
Loading