Skip to content

Commit 61169fc

Browse files
Sorts gate array to have deterministic gate selection (#16139)
* sorts gate array to have deterministic gate selection * changes array return and add test case * updates test case to generate expected result * running fmt and adding bugfix note * update bugfix notes
1 parent c4d8bee commit 61169fc

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

qiskit/circuit/random/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def random_clifford_circuit(num_qubits, num_gates, gates="all", seed=None):
708708
QuantumCircuit: constructed circuit
709709
"""
710710

711-
gates_1q = list(set(_BASIS_1Q.keys()) - {"v", "w", "id", "iden", "sinv"})
711+
gates_1q = [gate for gate in _BASIS_1Q if gate not in {"v", "w", "id", "iden", "sinv"}]
712712
gates_2q = list(_BASIS_2Q.keys())
713713

714714
if gates == "all":
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed :func:`.random_clifford_circuit` to produce deterministic results across multiple runs given the same ``seed``.
5+
6+
Fixes `#16138 <https://github.com/Qiskit/qiskit/issues/16138>`__.
7+

test/python/quantum_info/operators/symplectic/test_clifford.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
"""Tests for Clifford class."""
1515

16+
from collections import OrderedDict
17+
import os
18+
import subprocess
19+
import sys
20+
import textwrap
1621
import unittest
1722
import itertools
1823
import numpy as np
@@ -174,6 +179,29 @@ def test_append_1_qubit_gate(self):
174179
np.all(np.array(value_destabilizer == [target_destabilizer[gate_name]]))
175180
)
176181

182+
@combine(hash_seed=["1", "2", "10", "100"])
183+
def test_random_clifford_circuit_with_same_seed(self, hash_seed):
184+
random_clifford = random_clifford_circuit(
185+
num_qubits=10, num_gates=100, gates="all", seed=0
186+
).count_ops()
187+
188+
env = os.environ.copy()
189+
env["PYTHONHASHSEED"] = hash_seed
190+
191+
test_script = textwrap.dedent(
192+
"""
193+
from qiskit.circuit.random.utils import random_clifford_circuit
194+
cliff_circuit = random_clifford_circuit(num_qubits=10, num_gates=100, gates="all", seed=0)
195+
print(cliff_circuit.count_ops(),end="")
196+
"""
197+
)
198+
199+
result = subprocess.run(
200+
[sys.executable, "-c", test_script], env=env, capture_output=True, text=True, check=True
201+
)
202+
203+
self.assertEqual(result.stdout, random_clifford.__str__())
204+
177205
@combine(
178206
gate=[
179207
IGate(),

0 commit comments

Comments
 (0)