Skip to content

Commit a9b1c7e

Browse files
authored
Fix type comparisons to use isinstance (#430)
1 parent cbda8e5 commit a9b1c7e

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

recirq/fermi_hubbard/parameters.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,7 @@ def representative_parameters(self) -> 'FermiHubbardParameters':
596596
def equals_for_rescaling(self, other: 'FermiHubbardParameters') -> bool:
597597
if not isinstance(other, FermiHubbardParameters):
598598
return False
599-
if type(self.layout) != type(other.layout):
600-
return False
601-
if isinstance(self.layout, ZigZagLayout):
599+
if isinstance(self.layout, ZigZagLayout) and isinstance(other.layout, ZigZagLayout):
602600
interacting = not np.allclose(self.hamiltonian.u, 0.0)
603601
other_interacting = not np.allclose(other.hamiltonian.u, 0.0)
604602
return interacting == other_interacting

recirq/lattice_gauge/lattice_gauge_experiment.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def cnot_on_layer(
601601
cirq.Moment(cirq.CZ.on(qc, qt) for qc, qt in pairs_list),
602602
cirq.Moment(cirq.H.on_each(pair[1] for pair in pairs_list)),
603603
]
604-
elif type(depolarization_probability) == float:
604+
elif isinstance(depolarization_probability, float):
605605
return [
606606
cirq.Moment(cirq.H.on_each(pair[1] for pair in pairs_list)),
607607
cirq.Moment(cirq.CZ.on(qc, qt) for qc, qt in pairs_list),
@@ -611,7 +611,7 @@ def cnot_on_layer(
611611
),
612612
cirq.Moment(cirq.H.on_each(pair[1] for pair in pairs_list)),
613613
]
614-
elif type(depolarization_probability) == dict:
614+
elif isinstance(depolarization_probability, dict):
615615
# make sure there is a error for every pair.
616616
assert set(pairs_list).intersection(set(depolarization_probability.keys())) == set(
617617
pairs_list
@@ -742,7 +742,7 @@ def plot_qubit_polarization_values(
742742

743743
qubit_index += 1
744744

745-
elif type(plot_physical_qubits) == list:
745+
elif isinstance(plot_physical_qubits, list):
746746
# Plot X plaquette top qubits
747747
for row, col in plot_physical_qubits[0]:
748748
qubit_index = (row) * grid.cols + col
@@ -788,7 +788,7 @@ def plot_qubit_polarization_values(
788788
get_ancilla_patch(data = ancilla_states_data, qubit_index = qubit_index,row=row, col=col, x_basis=True, ancilla_cmap = ancilla_colormap, **qubit_kwargs)
789789
)
790790

791-
elif type(plot_ancillas) == list:
791+
elif isinstance(plot_ancillas, list):
792792
for row, col in plot_ancillas[0]:
793793
ax.add_patch(
794794
get_ancilla_patch(row=row, col=col, x_basis=False, **qubit_kwargs)

recirq/seniority_zero/data_processing/general.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def get_ls_echo_fidelity(
267267
postselect [bool]: whether or not to give the fidelity with
268268
postselection on half-filling.
269269
"""
270-
if type(frame) == cirq.Result:
270+
if isinstance(frame, cirq.Result):
271271
return get_ls_echo_fidelity(frame.data, qubits, group, postselect)
272272

273273
# Get the number of successful experiments
@@ -354,7 +354,7 @@ def get_signed_count_verified(
354354
correct_number: How many of all_qubits (excluding msmt_qubits)
355355
should be equal to 1.
356356
"""
357-
if type(frame) == cirq.Result:
357+
if isinstance(frame, cirq.Result):
358358
return get_signed_count_verified(frame.data, msmt_qubits, all_qubits, correct_number)
359359
other_qubit_names = [str(qubit) for qubit in all_qubits if qubit not in msmt_qubits]
360360
qubit_names = [str(qubit) for qubit in msmt_qubits]
@@ -388,7 +388,7 @@ def get_signed_count_postselected(
388388
all_qubits [List[cirq.GridQubit]] : list of all qubits
389389
correct_number: How many of all_qubits should be equal to 1.
390390
"""
391-
if type(frame) == cirq.Result:
391+
if isinstance(frame, cirq.Result):
392392
return get_signed_count_postselected(frame.data, msmt_qubits, all_qubits, correct_number)
393393
msmt_qubit_names = [str(qubit) for qubit in msmt_qubits]
394394
all_qubit_names = [str(qubit) for qubit in all_qubits]
@@ -416,7 +416,7 @@ def get_p0m_count_verified(
416416
correct_number: How many of all_qubits (excluding msmt_qubits)
417417
should be equal to 1.
418418
"""
419-
if type(frame) == cirq.Result:
419+
if isinstance(frame, cirq.Result):
420420
return get_p0m_count_verified(frame.data, msmt_qubits, all_qubits, correct_number)
421421
other_qubit_names = [str(qubit) for qubit in all_qubits if qubit not in msmt_qubits]
422422
qubit_names = [str(qubit) for qubit in msmt_qubits]

recirq/seniority_zero/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ def safe_concatenate_circuits(circuit1: cirq.Circuit, circuit2: cirq.Circuit) ->
415415
def merge_faster(gate1: cirq.Gate, gate2: cirq.Gate, tol: Optional[float]=1e-6) -> cirq.Gate:
416416
"""Merges single qubit gates to a phasedXZ gate, is faster than cirq when gates are rz."""
417417
if (
418-
type(gate1.gate) == cirq.PhasedXZGate
419-
and type(gate2.gate) == cirq.PhasedXZGate
418+
isinstance(gate1.gate, cirq.PhasedXZGate)
419+
and isinstance(gate2.gate, cirq.PhasedXZGate)
420420
and abs(gate2.gate.x_exponent) < tol
421421
):
422422
new_gate = cirq.PhasedXZGate(

0 commit comments

Comments
 (0)