diff --git a/qiskit/circuit/random/utils.py b/qiskit/circuit/random/utils.py index 1e7e1741275c..2f79992a703e 100644 --- a/qiskit/circuit/random/utils.py +++ b/qiskit/circuit/random/utils.py @@ -708,8 +708,8 @@ def random_clifford_circuit(num_qubits, num_gates, gates="all", seed=None): QuantumCircuit: constructed circuit """ - gates_1q = list(set(_BASIS_1Q.keys()) - {"v", "w", "id", "iden", "sinv"}) - gates_2q = list(_BASIS_2Q.keys()) + gates_1q = [gate for gate in _BASIS_1Q if gate not in {"v", "w", "id", "iden", "sinv"}] + gates_2q= list(_BASIS_2Q.keys()) if gates == "all": if num_qubits == 1: diff --git a/test/python/quantum_info/operators/symplectic/test_clifford.py b/test/python/quantum_info/operators/symplectic/test_clifford.py index d64b7c142620..276ee42a2fc8 100644 --- a/test/python/quantum_info/operators/symplectic/test_clifford.py +++ b/test/python/quantum_info/operators/symplectic/test_clifford.py @@ -13,6 +13,10 @@ """Tests for Clifford class.""" +import os +import subprocess +import sys +import textwrap import unittest import itertools import numpy as np @@ -174,6 +178,27 @@ def test_append_1_qubit_gate(self): np.all(np.array(value_destabilizer == [target_destabilizer[gate_name]])) ) + @combine(hash_seed=["1","2","10","100"]) + def test_random_clifford_circuit_with_same_seed(self, hash_seed): + env = os.environ.copy() + env["PYTHONHASHSEED"] = hash_seed + + test_script= textwrap.dedent(""" + from qiskit.circuit.random.utils import random_clifford_circuit + cliff_circuit = random_clifford_circuit(num_qubits=10, num_gates=100, gates="all", seed=0) + print(cliff_circuit.count_ops()) + """) + + result = subprocess.run( + [sys.executable, "-c", test_script], + env=env, + capture_output=True, + text=True, + check=True + ) + + self.assertEqual(result.stdout, "OrderedDict({'sxdg': 10, 'sdg': 10, 'iswap': 9, 'cz': 9, 'id': 9, 'h': 8, 'cy': 8, 'x': 6, 'ecr': 6, 'cx': 6, 'dcx': 4, 'swap': 4, 's': 4, 'y': 3, 'sx': 3, 'z': 1})\n") + @combine( gate=[ IGate(),