def test_repr_representation(): noise_model = NoiseModel() mock_noise = Mock(spec=Noise) mock_noise.__repr__ = Mock(return_value="n") mock_criteria = Mock(spec=Criteria) mock_criteria.__repr__ = Mock(return_value="c") noise_model.add_noise(mock_noise, mock_criteria) str_representation = noise_model.__repr__() assert str_representation == "{'instructions': [NoiseModelInstruction(noise=n, criteria=c)]}"
def test_simple_add_noise(): noise_model = NoiseModel() assert len(noise_model.instructions) == 0 mock_noise = Mock(spec=Noise) mock_criteria = Mock(spec=CircuitInstructionCriteria) noise_model.add_noise(mock_noise, mock_criteria) noise_list = noise_model.instructions assert len(noise_list) == 1 assert mock_noise == noise_list[0].noise assert mock_criteria == noise_list[0].criteria
def default_noise_model(): noise_list = [] criteria_list = [] noise_model = NoiseModel() for i in range(3): noise = Mock(spec=Noise) criteria = Mock(spec=CircuitInstructionCriteria) criteria.instruction_matches.return_value = i % 2 == 0 noise_model.add_noise(noise, criteria) noise_list.append(noise) criteria_list.append(criteria) return noise_model, noise_list, criteria_list
def test_str_representation(noise_types, expected_string): noise_model = NoiseModel() for noise_type in noise_types: for index in range(2): mock_noise = Mock(spec=Noise) mock_noise.__str__ = Mock(return_value=f"my_noise {index}") mock_criteria = Mock(spec=noise_type) mock_criteria.__str__ = Mock(return_value=f"my_criteria {index}") noise_model.add_noise(mock_noise, mock_criteria) str_representation = noise_model.__str__() assert str_representation == expected_string
def test_apply(): noise_model = (NoiseModel().add_noise(PauliChannel( 0.01, 0.02, 0.03), GateCriteria(Gate.I, [0, 1])).add_noise( Depolarizing(0.04), GateCriteria(Gate.H)).add_noise( TwoQubitDepolarizing(0.05), GateCriteria(Gate.CNot, [0, 1])).add_noise( PauliChannel(0.06, 0.07, 0.08), GateCriteria(Gate.H, [0, 1])).add_noise( Depolarizing(0.10), UnitaryGateCriteria(h_unitary(), 0)).add_noise( Depolarizing(0.06), ObservableCriteria(Observable.Z, 0)).add_noise( Depolarizing(0.09), QubitInitializationCriteria(0))) layer1 = Circuit().h(0).cnot(0, 1).sample(Observable.Z(), 0) layer2 = Circuit().unitary([0], h_unitary().to_matrix()) circuit = layer1 + layer2 noisy_circuit_from_circuit = noise_model.apply(circuit) expected_circuit = (Circuit().depolarizing(0, 0.09).h(0).depolarizing( 0, 0.04).pauli_channel(0, 0.06, 0.07, 0.08).cnot( 0, 1).two_qubit_depolarizing(0, 1, 0.05).unitary( [0], h_unitary().to_matrix()).depolarizing( 0, 0.10).apply_readout_noise(Depolarizing(0.06), 0).sample(Observable.Z(), 0)) assert noisy_circuit_from_circuit == expected_circuit
def test_serialization(): noise_model = (NoiseModel().add_noise(PauliChannel( 0.01, 0.02, 0.03), GateCriteria(Gate.I, [0, 1])).add_noise( Depolarizing(0.04), GateCriteria(Gate.H)).add_noise( Depolarizing(0.05), GateCriteria(Gate.CNot, [0, 1])).add_noise(PauliChannel(0.06, 0.07, 0.08), GateCriteria(Gate.H, [0, 1]))) serialized_model = noise_model.to_dict() deserialized_model = NoiseModel.from_dict(serialized_model) assert len(deserialized_model.instructions) == len( noise_model.instructions) for index, deserialized_item in enumerate(deserialized_model.instructions): assert noise_model.instructions[index].noise == deserialized_item.noise assert noise_model.instructions[ index].criteria == deserialized_item.criteria assert deserialized_model is not None
def test_apply_in_order(): noise_model = (NoiseModel().add_noise(Depolarizing(0.01), GateCriteria(Gate.H)).add_noise( Depolarizing(0.02), GateCriteria(Gate.H))) circuit = Circuit().h(0) noisy_circuit = noise_model.apply(circuit) expected_circuit = circuit.apply_gate_noise( [Depolarizing(0.01), Depolarizing(0.02)]) assert noisy_circuit == expected_circuit
def test_filter(gate, qubit, noise, noise_type, expected_length): noise_model = (NoiseModel().add_noise(PauliChannel( 0.01, 0.02, 0.03), GateCriteria(Gate.I, [0, 1])).add_noise( Depolarizing(0.04), GateCriteria(Gate.H)).add_noise( Depolarizing(0.05), GateCriteria(Gate.CNot, [0, 1])).add_noise( PauliChannel(0.06, 0.07, 0.08), GateCriteria(Gate.H, [0, 1])).add_noise( Depolarizing(0.09), GateCriteria(None, 0)).add_noise( Depolarizing(0.10), QubitInitializationCriteria([0, 1]))) result_model = noise_model.from_filter(qubit=qubit, gate=gate, noise=noise) assert len(result_model.instructions) == expected_length
def test_apply_to_circuit_list(): noise_model = NoiseModel() noise_model.add_noise(Mock(), Mock(spec=Criteria)) noise_model.apply([])
def test_add_invalid_criteria(): noise_model = NoiseModel() noise_model.add_noise(Mock(spec=Noise), Mock())
def test_remove_noise_at_invalid_index(): noise_model = NoiseModel() noise_model.remove_noise(index=0) assert not "should not get here"
GateCriteria(Gate.H)).add_noise( Depolarizing(0.02), GateCriteria(Gate.H))) circuit = Circuit().h(0) noisy_circuit = noise_model.apply(circuit) expected_circuit = circuit.apply_gate_noise( [Depolarizing(0.01), Depolarizing(0.02)]) assert noisy_circuit == expected_circuit @pytest.mark.parametrize( "noise_model, input_circuit, expected_circuit", [ ( # model with noise on H(0) NoiseModel().add_noise(Depolarizing(0.01), GateCriteria(Gate.H, 0)), # input circuit has an H gate on qubit 0 Circuit().h(0).cnot(0, 1), # expected circuit has noise on qubit 0 Circuit().h(0).depolarizing(0, 0.01).cnot(0, 1), ), ( # model with noise on H(0) NoiseModel().add_noise(Depolarizing(0.01), GateCriteria(Gate.H, 0)), # input circuit has two H gates on qubit 0 Circuit().h(0).h(0).cnot(0, 1), # expected circuit has noise on qubit 0 Circuit().h(0).depolarizing(0, 0.01).h(0).depolarizing( 0, 0.01).cnot(0, 1), ),