Skip to content

Commit

Permalink
Add missing tests and fixes
Browse files Browse the repository at this point in the history
Fix backend estimator test

Fix lint, backend estimator test

Include missing tests
  • Loading branch information
ElePT committed Nov 30, 2023
1 parent d0a9647 commit f227917
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion qiskit/providers/fake_provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,4 @@

# Configurable fake backend
from .utils.configurable_backend import ConfigurableFakeBackend
from .fake_generic import FakeGeneric
from .fake_generic import FakeGeneric, GenericTarget
18 changes: 9 additions & 9 deletions qiskit/providers/fake_provider/fake_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def __init__(
control_flow (bool): Flag to enable control flow directives on the backend
(defaults to False).
calibrate_gates (list[str] | None): List of gate names which should contain
default calibration entries. These must be a subset of ``basis_gates``.
calibrate_gates (list[str] | None): List of gate names (subset of basis_gates)
to add default calibration entries to.
rng (np.random.Generator): Optional fixed-seed generator for default random values.
"""
Expand Down Expand Up @@ -111,17 +111,18 @@ def __init__(
)

# ensure that Reset, Delay and Measure are in basis_gates
self._basis_gates = set(basis_gates)
self._basis_gates = basis_gates
for name in ["reset", "delay", "measure"]:
self._basis_gates.add(name)
if name not in self._basis_gates:
self._basis_gates.append(name)

# iterate over gates, generate noise params. from defaults
# and add instructions to target
for name in self._basis_gates:
if name not in self.supported_operations:
raise QiskitError(
f"Provided base gate {name} is not a supported "
f"operation ({self.supported_operations})."
f"operation ({self.supported_operations.keys()})."
)
gate = self.supported_operations[name]
noise_params = self.noise_defaults[name]
Expand Down Expand Up @@ -228,7 +229,7 @@ def add_calibrations_from_instruction_schedule_map(
"""Add calibration entries from provided pulse defaults to target.
Args:
defaults (PulseDefaults): pulse defaults with instruction schedule map
inst_map (InstructionScheduleMap): pulse defaults with instruction schedule map
Returns:
None
Expand Down Expand Up @@ -390,9 +391,8 @@ def __init__(
control_flow (bool): Flag to enable control flow directives on the backend
(defaults to False).
calibrate_gates (list[str] | None): List of gate names which should contain
default calibration entries (overriden if an ``instruction_schedule_map`` is
provided). These must be a subset of ``basis_gates``.
calibrate_gates (list[str] | None): List of gate names (subset of basis_gates)
to add default calibration entries to.
seed (int): Optional seed for generation of default values.
"""
Expand Down
4 changes: 2 additions & 2 deletions test/python/primitives/test_backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_layout(self, backend):
value = estimator.run(qc, op, shots=10000).result().values[0]

if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
self.assertEqual(value, -0.9154)
self.assertEqual(value, -0.916)
else:
self.assertEqual(value, -1)

Expand All @@ -410,7 +410,7 @@ def test_layout(self, backend):
value = estimator.run(qc, op, shots=10000).result().values[0]

if optionals.HAS_AER and not isinstance(backend, FakeBackendSimple):
self.assertEqual(value, -0.8908)
self.assertEqual(value, -0.8902)
else:
self.assertEqual(value, -1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from qiskit.test import QiskitTestCase
from qiskit.circuit.library import EfficientSU2
from qiskit.primitives import BackendEstimator
from qiskit.providers.fake_provider import FakeNairobiV2
from qiskit.providers.fake_provider import FakeGeneric
from qiskit.compiler.transpiler import transpile


Expand Down Expand Up @@ -1033,7 +1033,7 @@ def test_apply_layout_with_transpile(self):
"""Test the apply_layout method with a transpiler layout."""
psi = EfficientSU2(4, reps=4, entanglement="circular")
op = SparsePauliOp.from_list([("IIII", 1), ("IZZZ", 2), ("XXXI", 3)])
backend = FakeNairobiV2()
backend = FakeGeneric(num_qubits=7, basis_gates=["cx", "x", "id", "sx", "rz"])
transpiled_psi = transpile(psi, backend, optimization_level=3, seed_transpiler=12345)
permuted_op = op.apply_layout(transpiled_psi.layout)
identity_op = SparsePauliOp("I" * 7)
Expand All @@ -1048,15 +1048,15 @@ def test_permute_sparse_pauli_op_estimator_example(self):
"""Test using the apply_layout method with an estimator workflow."""
psi = EfficientSU2(4, reps=4, entanglement="circular")
op = SparsePauliOp.from_list([("IIII", 1), ("IZZZ", 2), ("XXXI", 3)])
backend = FakeNairobiV2()
backend = FakeGeneric(num_qubits=7, basis_gates=["cx", "x", "id", "sx", "rz"], seed=0)
backend.set_options(seed_simulator=123)
estimator = BackendEstimator(backend=backend, skip_transpilation=True)
thetas = list(range(len(psi.parameters)))
transpiled_psi = transpile(psi, backend, optimization_level=3)
permuted_op = op.apply_layout(transpiled_psi.layout)
job = estimator.run(transpiled_psi, permuted_op, thetas)
res = job.result().values
np.testing.assert_allclose(res, [1.35351562], rtol=0.5, atol=0.2)
np.testing.assert_allclose(res, [5.859375], rtol=0.5, atol=0.2)

def test_apply_layout_invalid_qubits_list(self):
"""Test that apply_layout with an invalid qubit count raises."""
Expand Down
8 changes: 4 additions & 4 deletions test/python/transpiler/test_normalize_rx_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
NormalizeRXAngle,
)
from qiskit.test import QiskitTestCase
from qiskit.providers.fake_provider import FakeBelemV2
from qiskit.providers.fake_provider import FakeGeneric
from qiskit.transpiler import Target
from qiskit.circuit.library.standard_gates import SXGate

Expand Down Expand Up @@ -60,7 +60,7 @@ def test_rz_added_for_negative_rotation_angles(self):
"""Check that RZ is added before and after RX,
if RX rotation angle is negative"""

backend = FakeBelemV2()
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"])
tp = NormalizeRXAngle(target=backend.target)

# circuit to transpile and test
Expand All @@ -83,7 +83,7 @@ def test_rz_added_for_negative_rotation_angles(self):
)
def test_angle_wrapping_works(self, raw_theta, correct_wrapped_theta):
"""Check that RX rotation angles are correctly wrapped to [0, pi]"""
backend = FakeBelemV2()
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"])
tp = NormalizeRXAngle(target=backend.target)

# circuit to transpile and test
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_quantize_angles(self, resolution, rx_angles, correct_num_of_cals):
"""Test that quantize_angles() adds a new calibration only if
the requested angle is not in the vicinity of the already generated angles.
"""
backend = FakeBelemV2()
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"])
tp = NormalizeRXAngle(backend.target, resolution_in_radian=resolution)

qc = QuantumCircuit(1)
Expand Down
2 changes: 1 addition & 1 deletion test/python/transpiler/test_sabre_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def test_integration_with_pass_manager(self):
qct_initial_layout = qct.layout.initial_layout
self.assertEqual(
[qct_initial_layout[q] for q in self.circuit.qubits],
[1, 6, 5, 10, 11, 12, 16, 17, 18, 13, 14, 9, 8, 3, 2, 0],
[8, 9, 14, 13, 18, 19, 17, 16, 11, 10, 5, 6, 1, 2, 3, 7],
)


Expand Down
10 changes: 6 additions & 4 deletions test/python/transpiler/test_unitary_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from qiskit import transpile
from qiskit.test import QiskitTestCase
from qiskit.providers.fake_provider import FakeVigo, FakeMumbaiFractionalCX, FakeBelemV2
from qiskit.providers.fake_provider import FakeVigo, FakeMumbaiFractionalCX, FakeGeneric
from qiskit.providers.fake_provider.fake_backend_v2 import FakeBackendV2, FakeBackend5QV2
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit.library import QuantumVolume
Expand Down Expand Up @@ -846,7 +846,8 @@ def test_single_qubit_with_target(self):
qc = QuantumCircuit(1)
qc.append(ZGate(), [qc.qubits[0]])
dag = circuit_to_dag(qc)
unitary_synth_pass = UnitarySynthesis(target=FakeBelemV2().target)
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"])
unitary_synth_pass = UnitarySynthesis(target=backend.target)
result_dag = unitary_synth_pass.run(dag)
result_qc = dag_to_circuit(result_dag)
self.assertEqual(qc, result_qc)
Expand All @@ -856,7 +857,8 @@ def test_single_qubit_identity_with_target(self):
qc = QuantumCircuit(1)
qc.unitary([[1.0, 0.0], [0.0, 1.0]], 0)
dag = circuit_to_dag(qc)
unitary_synth_pass = UnitarySynthesis(target=FakeBelemV2().target)
backend = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"])
unitary_synth_pass = UnitarySynthesis(target=backend.target)
result_dag = unitary_synth_pass.run(dag)
result_qc = dag_to_circuit(result_dag)
self.assertEqual(result_qc, QuantumCircuit(1))
Expand All @@ -866,7 +868,7 @@ def test_unitary_synthesis_with_ideal_and_variable_width_ops(self):
qc = QuantumCircuit(2)
qc.unitary(np.eye(4), [0, 1])
dag = circuit_to_dag(qc)
target = FakeBelemV2().target
target = FakeGeneric(num_qubits=5, basis_gates=["cx", "x", "id", "sx", "rz"]).target
target.add_instruction(IfElseOp, name="if_else")
target.add_instruction(ZGate())
target.add_instruction(ECRGate())
Expand Down
6 changes: 5 additions & 1 deletion test/python/transpiler/test_vf2_post_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ def test_2q_circuit_5q_backend_max_trials(self):
def test_best_mapping_ghz_state_full_device_multiple_qregs_v2(self):
"""Test best mappings with multiple registers"""
backend = FakeGeneric(
num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=LIMA_CM
num_qubits=5, basis_gates=["cx", "id", "rz", "sx", "x"], coupling_map=LIMA_CM, seed=123
)

for i in range(len(backend.target.instructions)):
print(backend.target.instructions[i], backend.target.instruction_properties(i))

qr_a = QuantumRegister(2)
qr_b = QuantumRegister(3)
qc = QuantumCircuit(qr_a, qr_b)
Expand Down

0 comments on commit f227917

Please sign in to comment.