def test_identity_gates_are_replaced_with_zero_angle_rotation(self): identity_gate_indices = [0, 2] original_circuit = circuits.Circuit( [circuits.I(0), circuits.X(1), circuits.I(2), circuits.RX(0)(2)]) compatible_circuit = make_circuit_qhipster_compatible(original_circuit) assert all(compatible_circuit.operations[i].gate == circuits.RX(0) and compatible_circuit.operations[i].qubit_indices == original_circuit.operations[i].qubit_indices for i in identity_gate_indices)
def test_circuit_with_only_supported_gates_is_not_changed(self): original_circuit = circuits.Circuit([ circuits.X(0), circuits.RX(np.pi)(2), circuits.SWAP(3, 0), circuits.RY(0.5).controlled(1)(0, 2), ]) assert make_circuit_qhipster_compatible( original_circuit) == original_circuit
def test_supported_gates_are_left_unchanged(self): supported_gate_indices = [1, 3] original_circuit = circuits.Circuit( [circuits.I(0), circuits.X(1), circuits.I(2), circuits.RX(0)(2)]) compatible_circuit = make_circuit_qhipster_compatible(original_circuit) all(compatible_circuit.operations[i] == original_circuit.operations[i] for i in supported_gate_indices)
class TestGeneratingCircuitSequence: @pytest.mark.parametrize( "repeated_circuit, different_circuit, length, position, expected_result", [ ( circuits.Circuit([circuits.X(0), circuits.Y(1)]), circuits.Circuit([circuits.Z(1)]), 5, 1, circuits.Circuit([ *[circuits.X(0), circuits.Y(1)], circuits.Z(1), *([circuits.X(0), circuits.Y(1)] * 3), ]), ), ( circuits.Circuit([circuits.RX(0.5)(1)]), circuits.Circuit([circuits.CNOT(0, 2)]), 3, 0, circuits.Circuit([ circuits.CNOT(0, 2), circuits.RX(0.5)(1), circuits.RX(0.5)(1) ]), ), ( circuits.Circuit([circuits.RX(0.5)(1)]), circuits.Circuit([circuits.CNOT(0, 2)]), 3, 2, circuits.Circuit([ circuits.RX(0.5)(1), circuits.RX(0.5)(1), circuits.CNOT(0, 2), ]), ), ], ) def test_produces_correct_sequence(self, repeated_circuit, different_circuit, position, length, expected_result): actual_result = _generate_circuit_sequence(repeated_circuit, different_circuit, length, position) assert actual_result == expected_result @pytest.mark.parametrize("length, invalid_position", [(5, 5), (4, 6)]) def test_raises_error_if_position_is_larger_or_equal_to_length( self, length, invalid_position): repeated_circuit = circuits.Circuit([circuits.X(0)]) different_circuit = circuits.Circuit([circuits.Y(0)]) with pytest.raises(ValueError): _generate_circuit_sequence(repeated_circuit, different_circuit, length, invalid_position)
def make_circuit_qhipster_compatible(circuit: circuits.Circuit): unsupported_operations = [ op for op in circuit.operations if op.gate.name in QHIPSTER_UNSUPPORTED_GATES ] if unsupported_operations: raise NotImplementedError( "ISWAP gates and two-qubit Pauli rotations are not supported by qHipster " f"integration. Offending operations: {unsupported_operations}.") return circuits.Circuit( operations=[ circuits.RX(0)(*op.qubit_indices) if op.gate.name == "I" else op for op in circuit.operations ], n_qubits=circuit.n_qubits, )
class TestConvertingCircuitToSimplifiedQasm: @pytest.mark.parametrize( "circuit, expected_qasm", [ (circuits.Circuit(), "0\n"), ( circuits.Circuit([circuits.X(0), circuits.Y(2), circuits.Z(1)]), "\n".join(["3", "X 0", "Y 2", "Z 1"]), ), ( circuits.Circuit([circuits.X(0), circuits.Z(4)]), "\n".join(["5", "X 0", "Z 4"]), ), ( circuits.Circuit([circuits.X(4), circuits.Z(0)]), "\n".join(["5", "X 4", "Z 0"]), ), ( circuits.Circuit([circuits.X(4), circuits.CNOT(0, 3)]), "\n".join(["5", "X 4", "CNOT 0 3"]), ), ( circuits.Circuit([circuits.RX(np.pi)(1), circuits.RZ(0.5)(3)]), "\n".join([ "4", "Rx 3.14159265358979311600 1", "Rz 0.50000000000000000000 3" ]), ), ], ) def test_converting_circuit_to_qasm_emits_correct_string( self, circuit, expected_qasm): assert convert_to_simplified_qasm(circuit) == expected_qasm