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

fix empty circuit error #303

Merged
merged 1 commit into from
Jan 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/itensor/quri_parts/itensor/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def _sample(
qubits = circuit.qubit_count
s: juliacall.VectorValue = jl.siteinds("Qubit", qubits)
psi: juliacall.AnyValue = jl.init_state(s, qubits)
if len(circuit.gates) == 0:
return Counter({0: shots})
circuit_ops = convert_circuit(circuit, s)
psi = jl.apply(circuit_ops, psi, **kwargs)
if any(k in kwargs for k in ["mindim", "maxdim", "cutoff"]):
Expand Down
20 changes: 17 additions & 3 deletions packages/itensor/tests/itensor/sampler/test_itensor_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def test_sampler(self, qubits: int, shots: int, sampler_kwargs: Any) -> None:
assert all(c >= 0 for c in counts.values())
assert sum(counts.values()) == shots

# sample on empty circuit
circuit = QuantumCircuit(qubits)
counts = sampler(circuit, shots)

assert len(counts) == 1
assert next(iter(counts)) == 0
assert counts[0] == shots


class TestITensorMPSConcurrentSampler:
@pytest.mark.skip(reason="This test is too slow.")
Expand All @@ -68,16 +76,22 @@ def test_concurrent_sampler(self, sampler_kwargs: Any) -> None:
circuit1 = circuit()
circuit2 = circuit()
circuit2.add_X_gate(3)
circuit3 = QuantumCircuit(4)
with ProcessPoolExecutor(
max_workers=2, mp_context=get_context("spawn")
max_workers=3, mp_context=get_context("spawn")
) as executor:
sampler = create_itensor_mps_concurrent_sampler(
executor, 2, **sampler_kwargs
executor, 3, **sampler_kwargs
)
results = list(
sampler([(circuit1, 1000), (circuit2, 2000), (circuit3, 4000)])
)
results = list(sampler([(circuit1, 1000), (circuit2, 2000)]))
assert set(results[0]) == {0b1001, 0b1011}
assert all(c >= 0 for c in results[0].values())
assert sum(results[0].values()) == 1000
assert set(results[1]) == {0b0001, 0b0011}
assert all(c >= 0 for c in results[1].values())
assert sum(results[1].values()) == 2000
assert set(results[2]) == {0b0000}
assert all(c >= 0 for c in results[2].values())
assert sum(results[2].values()) == 4000