示例#1
0
    def test_noise_model_basis_gates(self):
        """Test noise model basis_gates"""
        basis_gates = ['u1', 'u2', 'u3', 'cx']
        model = NoiseModel(basis_gates)
        target = sorted(basis_gates)
        self.assertEqual(model.basis_gates, target)

        # Check adding readout errors doesn't add to basis gates
        model = NoiseModel(basis_gates)
        target = sorted(basis_gates)
        model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]], False)
        self.assertEqual(model.basis_gates, target)
        model.add_readout_error([[0.9, 0.1], [0, 1]], [2], False)
        self.assertEqual(model.basis_gates, target)

        # Check a reset instruction error isn't added to basis gates
        model = NoiseModel(basis_gates)
        target = sorted(basis_gates)
        model.add_all_qubit_quantum_error(reset_error(0.2), ['reset'], False)
        self.assertEqual(model.basis_gates, target)

        # Check a non-standard gate isn't added to basis gates
        model = NoiseModel(basis_gates)
        target = sorted(basis_gates)
        model.add_all_qubit_quantum_error(reset_error(0.2), ['label'], False)
        self.assertEqual(model.basis_gates, target)

        # Check a standard gate is added to basis gates
        model = NoiseModel(basis_gates)
        target = sorted(basis_gates + ['h'])
        model.add_all_qubit_quantum_error(reset_error(0.2), ['h'], False)
        self.assertEqual(model.basis_gates, target)
    def test_reduce_noise_model(self):
        """Test reduction mapping of noise model."""
        error1 = depolarizing_error(0.5, 1)
        error2 = depolarizing_error(0.5, 2)
        roerror1 = [[0.9, 0.1], [0.5, 0.5]]
        roerror2 = [[0.8, 0.2, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                    [0, 0, 0.1, 0.9]]

        model = NoiseModel()
        model.add_all_qubit_quantum_error(error1, ['u3'], False)
        model.add_quantum_error(error1, ['u3'], [1], False)
        with self.assertWarns(DeprecationWarning):
            model.add_nonlocal_quantum_error(error2, ['cx'], [2, 0], [3, 1],
                                             False)
        model.add_all_qubit_readout_error(roerror1, False)
        model.add_readout_error(roerror2, [0, 2], False)

        remapped_model = remap_noise_model(model, [0, 1, 2],
                                           discard_qubits=True,
                                           warnings=False)
        target = NoiseModel()
        target.add_all_qubit_quantum_error(error1, ['u3'], False)
        target.add_quantum_error(error1, ['u3'], [1], False)
        target.add_all_qubit_readout_error(roerror1, False)
        target.add_readout_error(roerror2, [0, 2], False)
        self.assertEqual(remapped_model, target)
def noise_model_depolarizing(error, measure=True, thermal=True):
    """
    Creates error for depolarizing channel
    :param error: Probability of depolarizing channel
    :param measure: True or False, whether we include readout errors with probability 10 * error
    :param thermal: True or False, whether we include thermal relaxation, see noise_model_thermal
    :return: noise model for the error
    """
    noise_model = NoiseModel()
    depolarizing = depolarizing_error(error, 1)
    cdepol_error = depolarizing_error(2 * error, 2)
    noise_model.add_all_qubit_quantum_error(cdepol_error, ['cx'],
                                            warnings=False)
    noise_model.add_all_qubit_quantum_error(depolarizing, ["u1", "u2", "u3"])

    if measure:
        measure_error = 10 * error
        measure_error = array([[1 - measure_error, measure_error],
                               [measure_error, 1 - measure_error]])
        noise_model.add_all_qubit_readout_error(measure_error)

    if thermal:
        thermal = thermal_relaxation_error(1.5, 1.2, error)
        cthermal = thermal_relaxation_error(1.5, 1.2, 2 * error)
        cthermal = cthermal.tensor(cthermal)
        noise_model.add_all_qubit_quantum_error(cthermal, ['cx'],
                                                warnings=False)
        noise_model.add_all_qubit_quantum_error(thermal, ["u1", "u2", "u3"],
                                                warnings=False)

    return noise_model
示例#4
0
    def test_measure_sampling_with_quantum_noise(self):
        """Test QasmSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))
        for gate, (num_qubits, gate_error) in depolarizing.items():
            noise_model.add_all_qubit_quantum_error(
                depolarizing_error(gate_error, num_qubits), gate)

        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        result = self.SIMULATOR.run(
            qobj, noise_model=noise_model,
            backend_options=self.BACKEND_OPTS).result()
        self.assertTrue(getattr(result, 'success', False))
        sampling = (self.BACKEND_OPTS.get(
            "method", "automatic").startswith("density_matrix"))
        self.compare_result_metadata(result, circuits, "measure_sampling",
                                     sampling)
示例#5
0
    def test_noise_model_noise_instructions(self):
        """Test noise instructions"""
        model = NoiseModel()
        target = []
        self.assertEqual(model.noise_instructions, target)

        # Check a non-standard gate is added to noise instructions
        model = NoiseModel()
        model.add_all_qubit_quantum_error(reset_error(0.2), ['label'], False)
        target = ['label']
        self.assertEqual(model.noise_instructions, target)

        # Check a standard gate is added to noise instructions
        model = NoiseModel()
        model.add_all_qubit_quantum_error(reset_error(0.2), ['h'], False)
        target = ['h']
        self.assertEqual(model.noise_instructions, target)

        # Check a reset is added to noise instructions
        model = NoiseModel()
        model.add_all_qubit_quantum_error(reset_error(0.2), ['reset'], False)
        target = ['reset']
        self.assertEqual(model.noise_instructions, target)

        # Check a measure is added to noise instructions for readout error
        model = NoiseModel()
        model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]], False)
        target = ['measure']
        self.assertEqual(model.noise_instructions, target)
def readout_error_noise_models():
    """Readout error test circuit noise models."""
    noise_models = []

    # 1-qubit readout error on qubit 0
    noise_model = NoiseModel()
    noise_model.add_readout_error(ROERROR_1Q, [0])
    noise_models.append(noise_model)

    # 1-qubit readout error on qubit 1
    noise_model = NoiseModel()
    noise_model.add_readout_error(ROERROR_1Q, [1])
    noise_models.append(noise_model)

    # 1-qubit readout error on qubit 1
    noise_model = NoiseModel()
    noise_model.add_all_qubit_readout_error(ROERROR_1Q)
    noise_models.append(noise_model)

    # 2-qubit readout error on qubits 0,1
    noise_model = NoiseModel()
    noise_model.add_readout_error(ROERROR_2Q, [0, 1])
    noise_models.append(noise_model)

    return noise_models
示例#7
0
    def test_readout_error_all_qubit(self):
        """Test 100% readout error on all qubits"""

        # Test circuit: ideal bell state
        qr = QuantumRegister(2, 'qr')
        cr = ClassicalRegister(2, 'cr')
        circuit = QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        # Ensure qubit 0 is measured before qubit 1
        circuit.barrier(qr)
        circuit.measure(qr[0], cr[0])
        circuit.barrier(qr)
        circuit.measure(qr[1], cr[1])
        backend = QasmSimulator()

        # Asymetric readout error on qubit-0 only
        probs_given0 = [0.9, 0.1]
        probs_given1 = [0.3, 0.7]
        noise_model = NoiseModel()
        noise_model.add_all_qubit_readout_error([probs_given0, probs_given1])

        # Expected counts
        shots = 2000
        p00 = 0.5 * (probs_given0[0] ** 2 + probs_given1[0] ** 2)
        p01 = 0.5 * (probs_given0[0] * probs_given0[1] + probs_given1[0] * probs_given1[1])
        p10 = 0.5 * (probs_given0[0] * probs_given0[1] + probs_given1[0] * probs_given1[1])
        p11 = 0.5 * (probs_given0[1] ** 2 + probs_given1[1] ** 2)
        target = target = {'0x0': p00 * shots, '0x1': p01 * shots,
                           '0x2': p10 * shots, '0x3': p11 * shots}
        qobj = compile([circuit], backend, shots=shots,
                       basis_gates=noise_model.basis_gates)
        result = backend.run(qobj, noise_model=noise_model).result()
        self.is_completed(result)
        self.compare_counts(result, [circuit], [target], delta=0.05 * shots)
示例#8
0
def get_data_point(circ, theta, phi, lam, readout_params, depol_param, thermal_params, shots):
    """Generate a dict datapoint with the given circuit and noise parameters"""
    U3_gate_length = 7.111111111111112e-08

    # extract parameters
    (p0_0, p1_0), (p0_1, p1_1) = readout_params
    depol_prob = depol_param
    t1, t2, population = thermal_params

    # Add Readout and Quantum Errors
    noise_model = NoiseModel()
    noise_model.add_all_qubit_readout_error(ReadoutError(readout_params))
    noise_model.add_all_qubit_quantum_error(depolarizing_error(depol_param, 1), 'u3', warnings=False)
    noise_model.add_all_qubit_quantum_error(
        thermal_relaxation_error(t1, t2, U3_gate_length, excited_state_population=population), 'u3',
        warnings=False)
    job = execute(circ, QasmSimulator(), shots=shots, noise_model=noise_model)
    result = job.result()

    # add data point to DataFrame
    data_point = {'theta': theta,
                  'phi': phi,
                  'lam': lam,
                  'p0_0': p0_0,
                  'p1_0': p1_0,
                  'p0_1': p0_1,
                  'p1_1': p1_1,
                  'depol_prob': depol_prob,
                  't1': t1,
                  't2': t2,
                  'population': population,
                  'E': result.get_counts(0).get('1', 0) / shots}

    return data_point
示例#9
0
    def test_measure_sampling_with_quantum_noise(self):
        """Test QasmSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))
        for gate, (num_qubits, gate_error) in depolarizing.items():
            noise_model.add_all_qubit_quantum_error(
                depolarizing_error(gate_error, num_qubits), gate)

        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        result = self.SIMULATOR.run(
            qobj, noise_model=noise_model,
            backend_options=self.BACKEND_OPTS).result()
        self.is_completed(result)

        for res in result.results:
            self.assertIn("measure_sampling", res.metadata)
            self.assertEqual(res.metadata["measure_sampling"], False)

        # Test sampling was disabled
        for res in result.results:
            self.assertIn("measure_sampling", res.metadata)
            self.assertEqual(res.metadata["measure_sampling"], False)
def create_high_noise_model():
    """
    create high noise model with depolarizing error, thermal error and readout error
    Returns:
        NoiseModel: noise model
    """
    noise_model = NoiseModel()
    p1q = 0.004
    p2q = 0.05
    depol_sx = depolarizing_error(p1q, 1)
    depol_x = depolarizing_error(p1q, 1)
    depol_cx = depolarizing_error(p2q, 2)

    # Add T1/T2 noise to the simulation
    t_1 = 110e2
    t_2 = 120e2
    gate1q = 50
    gate2q = 100
    termal_sx = thermal_relaxation_error(t_1, t_2, gate1q)
    termal_x = thermal_relaxation_error(t_1, t_2, gate1q)
    termal_cx = thermal_relaxation_error(t_1, t_2, gate2q).tensor(
        thermal_relaxation_error(t_1, t_2, gate2q)
    )

    noise_model.add_all_qubit_quantum_error(depol_sx.compose(termal_sx), "sx")
    noise_model.add_all_qubit_quantum_error(depol_x.compose(termal_x), "x")
    noise_model.add_all_qubit_quantum_error(depol_cx.compose(termal_cx), "cx")

    read_err = readout_error.ReadoutError([[0.98, 0.02], [0.04, 0.96]])
    noise_model.add_all_qubit_readout_error(read_err)
    return noise_model
def noise_model_phase(error, measure=True, thermal=True):
    """
    Creates error for phase flip
    :param error: Probability of happening a phaseflip
    :param measure: True or False, whether we include readout errors with probability 10 * error
    :param thermal: True or False, whether we include thermal relaxation, see noise_model_thermal
    :return: noise model for the error
    """
    noise_model = NoiseModel()  #basis_gates=['id', 'u2', 'u3', 'cx'])
    phase_error = error
    cz_error = 2 * error
    phase_flip = pauli_error([('Z', phase_error), ('I', 1 - phase_error)])
    cz_error = pauli_error([('Z', cz_error), ('I', 1 - cz_error)])
    cz_error = cz_error.tensor(cz_error)
    noise_model.add_all_qubit_quantum_error(cz_error, ['cx'], warnings=False)
    noise_model.add_all_qubit_quantum_error(phase_flip, ["u1", "u2", "u3"])

    if measure:
        measure_error = 10 * error
        measure_error = array([[1 - measure_error, measure_error],
                               [measure_error, 1 - measure_error]])
        noise_model.add_all_qubit_readout_error(measure_error)

    if thermal:
        thermal = thermal_relaxation_error(1.5, 1.2, error)
        cthermal = thermal_relaxation_error(1.5, 1.2, 2 * error)
        cthermal = cthermal.tensor(cthermal)
        noise_model.add_all_qubit_quantum_error(cthermal, ['cx'],
                                                warnings=False)
        noise_model.add_all_qubit_quantum_error(thermal, ["u1", "u2", "u3"],
                                                warnings=False)

    return noise_model
def noise_model_bit(error, measure=True, thermal=True):
    """
    Creates error for bit flip
    :param error: Probability of happening a bitflip
    :param measure: True or False, whether we include readout errors with probability 10 * error
    :param thermal: True or False, whether we include thermal relaxation, see noise_model_thermal
    :return: noise model for the error
    """
    noise_model = NoiseModel()
    flip_error = error
    cnot_error = 2 * error
    bit_flip = pauli_error([('X', flip_error), ('I', 1 - flip_error)])
    cnot_flip = pauli_error([('X', cnot_error), ('I', 1 - cnot_error)])
    cnot_error = cnot_flip.tensor(cnot_flip)
    noise_model.add_all_qubit_quantum_error(bit_flip, ["u1", "u2", "u3"])
    noise_model.add_all_qubit_quantum_error(cnot_error, ['cx'], warnings=False)

    if measure:
        measure_error = 10 * error
        measure_error = array([[1 - measure_error, measure_error],
                               [measure_error, 1 - measure_error]])
        noise_model.add_all_qubit_readout_error(measure_error)

    if thermal:
        thermal = thermal_relaxation_error(1.5, 1.2, error)
        cthermal = thermal_relaxation_error(1.5, 1.2, 2 * error)
        cthermal = cthermal.tensor(cthermal)
        noise_model.add_all_qubit_quantum_error(cthermal, ['cx'],
                                                warnings=False)
        noise_model.add_all_qubit_quantum_error(thermal, ["u1", "u2", "u3"],
                                                warnings=False)

    return noise_model
示例#13
0
    def test_remap_all_qubit_readout_errors(self):
        """Test remapping of all-qubit readout errors."""
        model = NoiseModel()
        error1 = [[0.9, 0.1], [0.5, 0.5]]
        model.add_all_qubit_readout_error(error1, False)

        remapped_model = remap_noise_model(model, [[0, 1], [1, 2], [2, 0]], warnings=False)
        self.assertEqual(remapped_model, model)
示例#14
0
 def noise_model(self):
     readout_error = [0.01, 0.1]
     depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
     noise = NoiseModel()
     readout = [[1.0 - readout_error[0], readout_error[0]],
                [readout_error[1], 1.0 - readout_error[1]]]
     noise.add_all_qubit_readout_error(ReadoutError(readout))
     for gate, (num_qubits, gate_error) in depolarizing.items():
         noise.add_all_qubit_quantum_error(
             depolarizing_error(gate_error, num_qubits), gate)
         return noise
示例#15
0
 def noise_model_kraus(self):
     """ Creates a new noise model for testing purposes """
     readout_error = [0.01, 0.1]
     params = {'u3': (1, 0.001), 'cx': (2, 0.02)}
     noise = NoiseModel()
     readout = [[1.0 - readout_error[0], readout_error[0]],
                [readout_error[1], 1.0 - readout_error[1]]]
     noise.add_all_qubit_readout_error(ReadoutError(readout))
     for gate, (num_qubits, gate_error) in params.items():
         noise.add_all_qubit_quantum_error(
             amplitude_damping_error(gate_error, num_qubits), gate)
         return noise
def noise_model_measure(error, measure=True, thermal=True):
    """
    Creates errors for readout
    :param error: Probability of occuring a readout error / 10. This factor is added to maintain comparability with the rest of the code
    :return: noise model for the error
    """
    noise_model = NoiseModel()
    measure_error = 10 * error
    measure_error = array([[1 - measure_error, measure_error],
                           [measure_error, 1 - measure_error]])
    noise_model.add_all_qubit_readout_error(measure_error)

    return noise_model
def get_data_point(theta, phi, lam, readout_params, depol_param,
                   thermal_params, shots):
    """Generate a dict datapoint with the given circuit and noise parameters"""
    U3_gate_length = 7.111111111111112e-08

    circ = QuantumCircuit(1, 1)
    circ.append(U3Gate(theta, phi, lam), [0])
    circ.measure(0, 0)
    new_circ = qiskit.compiler.transpile(circ,
                                         basis_gates=['u3'],
                                         optimization_level=0)

    # extract parameters
    (p0_0, p1_0), (p0_1, p1_1) = readout_params
    depol_prob = depol_param
    t1, t2, population = thermal_params

    noise_model = NoiseModel()

    # Add Readout and Quantum Errors
    noise_model.add_all_qubit_readout_error(ReadoutError(readout_params))
    noise_model.add_all_qubit_quantum_error(depolarizing_error(depol_param, 1),
                                            'u3',
                                            warnings=False)
    noise_model.add_all_qubit_quantum_error(thermal_relaxation_error(
        t1, t2, U3_gate_length, population),
                                            'u3',
                                            warnings=False)
    job = execute(circ, QasmSimulator(), shots=shots, noise_model=noise_model)
    result = job.result()

    # add data point to DataFrame
    data_point = {
        'theta': theta,
        'phi': phi,
        'lam': lam,
        'p0_0': p0_0,
        'p1_0': p1_0,
        'p0_1': p0_1,
        'p1_1': p1_1,
        'depol_prob': depol_prob,
        't1': t1,
        't2': t2,
        'population': population,
        'E': result.get_counts(0).get('0', 0) / shots
    }

    return data_point
    def test_measure_sampling_with_readouterror(self):
        """Test QasmSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))

        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        result = self.SIMULATOR.run(qobj,
                                    noise_model=noise_model,
                                    **self.BACKEND_OPTS).result()
        self.assertSuccess(result)
    def test_measure_sampling_with_readouterror(self, method, device):
        """Test AerSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))

        backend = self.backend(method=method,
                               device=device,
                               noise_model=noise_model)
        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        result = backend.run(circuits, shots=shots).result()
        self.assertSuccess(result)
        self.compare_result_metadata(result, circuits, "measure_sampling",
                                     True)
示例#20
0
    def test_transform_noise(self):
        org_error = reset_error(0.2)
        new_error = pauli_error([("I", 0.5), ("Z", 0.5)])

        model = NoiseModel()
        model.add_all_qubit_quantum_error(org_error, ['x'])
        model.add_quantum_error(org_error, ['sx'], [0])
        model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]])

        def map_func(noise):
            return new_error if noise == org_error else None

        actual = transform_noise_model(model, map_func)

        expected = NoiseModel()
        expected.add_all_qubit_quantum_error(new_error, ['x'])
        expected.add_quantum_error(new_error, ['sx'], [0])
        expected.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]])

        self.assertEqual(actual, expected)
示例#21
0
    def test_noise_models_equal(self):
        """Test two noise models are Equal"""
        roerror = [[0.9, 0.1], [0.5, 0.5]]
        error1 = pauli_error([['X', 1]], standard_gates=False)
        error2 = pauli_error([['X', 1]], standard_gates=True)

        model1 = NoiseModel()
        model1.add_all_qubit_quantum_error(error1, ['u3'], False)
        model1.add_quantum_error(error1, ['u3'], [2], False)
        model1.add_nonlocal_quantum_error(error1, ['cx'], [0, 1], [3], False)
        model1.add_all_qubit_readout_error(roerror, False)
        model1.add_readout_error(roerror, [0], False)

        model2 = NoiseModel()
        model2.add_all_qubit_quantum_error(error2, ['u3'], False)
        model2.add_quantum_error(error2, ['u3'], [2], False)
        model2.add_nonlocal_quantum_error(error2, ['cx'], [0, 1], [3], False)
        model2.add_all_qubit_readout_error(roerror, False)
        model2.add_readout_error(roerror, [0], False)
        self.assertEqual(model1, model2)
    def test_measure_sampling_with_quantum_noise(self):
        """Test QasmSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))
        for gate, (num_qubits, gate_error) in depolarizing.items():
            noise_model.add_all_qubit_quantum_error(
                depolarizing_error(gate_error, num_qubits), gate)

        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        result = self.SIMULATOR.run(qobj,
                                    noise_model=noise_model,
                                    **self.BACKEND_OPTS).result()
        self.assertSuccess(result)
def noise_model_thermal(error, measure=False, thermal=False):
    """
    Creates error for thermal relaxation for T1, T2 = 1.5, 1.2
    :param error: time of gate. Normalized to be equal to the error in all other functions
    :param measure: True or False, whether we include readout errors with probability 10 * error
    :return: noise model for the error
    """
    noise_model = NoiseModel()
    thermal = thermal_relaxation_error(1.5, 1.2, error)
    cthermal = thermal_relaxation_error(1.5, 1.2, 2 * error)
    cthermal = cthermal.tensor(cthermal)
    noise_model.add_all_qubit_quantum_error(cthermal, ['cx'], warnings=False)
    noise_model.add_all_qubit_quantum_error(thermal, ["u1", "u2", "u3"])

    if measure:
        measure_error = 10 * error
        measure_error = array([[1 - measure_error, measure_error],
                               [measure_error, 1 - measure_error]])
        noise_model.add_all_qubit_readout_error(measure_error)

    return noise_model
示例#24
0
    def test_measure_sampling_with_readouterror(self):
        """Test QasmSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))

        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        result = self.SIMULATOR.run(
            qobj, noise_model=noise_model,
            backend_options=self.BACKEND_OPTS).result()
        self.is_completed(result)

        # Test sampling was disabled
        for res in result.results:
            self.assertIn("measure_sampling", res.metadata)
            self.assertEqual(res.metadata["measure_sampling"], True)
示例#25
0
    def test_noise_models_equal(self):
        """Test two noise models are Equal"""
        roerror = [[0.9, 0.1], [0.5, 0.5]]
        error1 = kraus_error([np.diag([1, 0]), np.diag([0, 1])])
        error2 = pauli_error([("I", 0.5), ("Z", 0.5)])

        model1 = NoiseModel()
        model1.add_all_qubit_quantum_error(error1, ['u3'], False)
        model1.add_quantum_error(error1, ['u3'], [2], False)
        with self.assertWarns(DeprecationWarning):
            model1.add_nonlocal_quantum_error(error1, ['cx'], [0, 1], [3], False)
        model1.add_all_qubit_readout_error(roerror, False)
        model1.add_readout_error(roerror, [0], False)

        model2 = NoiseModel()
        model2.add_all_qubit_quantum_error(error2, ['u3'], False)
        model2.add_quantum_error(error2, ['u3'], [2], False)
        with self.assertWarns(DeprecationWarning):
            model2.add_nonlocal_quantum_error(error2, ['cx'], [0, 1], [3], False)
        model2.add_all_qubit_readout_error(roerror, False)
        model2.add_readout_error(roerror, [0], False)
        self.assertEqual(model1, model2)
示例#26
0
    def test_noise_model_noise_qubits(self):
        """Test noise instructions"""
        model = NoiseModel()
        target = []
        self.assertEqual(model.noise_qubits, target)

        # Check adding a default error isn't added to noise qubits
        model = NoiseModel()
        model.add_all_qubit_quantum_error(pauli_error([['XX', 1]]), ['label'],
                                          False)
        target = []
        self.assertEqual(model.noise_qubits, target)

        # Check adding a local error adds to noise qubits
        model = NoiseModel()
        model.add_quantum_error(pauli_error([['XX', 1]]), ['label'], [1, 0],
                                False)
        target = sorted([0, 1])
        self.assertEqual(model.noise_qubits, target)

        # Check adding a non-local error adds to noise qubits
        model = NoiseModel()
        with self.assertWarns(DeprecationWarning):
            model.add_nonlocal_quantum_error(pauli_error([['XX', 1]]),
                                             ['label'], [0], [1, 2], False)
        target = sorted([0, 1, 2])
        self.assertEqual(model.noise_qubits, target)

        # Check adding a default error isn't added to noise qubits
        model = NoiseModel()
        model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]], False)
        target = []
        self.assertEqual(model.noise_qubits, target)

        # Check adding a local error adds to noise qubits
        model = NoiseModel()
        model.add_readout_error([[0.9, 0.1], [0, 1]], [2], False)
        target = [2]
        self.assertEqual(model.noise_qubits, target)
    def test_measure_sampling_with_quantum_noise(self, method, device):
        """Test AerSimulator measure with deterministic counts with sampling and readout-error"""
        readout_error = [0.01, 0.1]
        noise_model = NoiseModel()
        depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)}
        readout = [[1.0 - readout_error[0], readout_error[0]],
                   [readout_error[1], 1.0 - readout_error[1]]]
        noise_model.add_all_qubit_readout_error(ReadoutError(readout))
        for gate, (num_qubits, gate_error) in depolarizing.items():
            noise_model.add_all_qubit_quantum_error(
                depolarizing_error(gate_error, num_qubits), gate)

        backend = self.backend(method=method,
                               device=device,
                               noise_model=noise_model)
        shots = 1000
        circuits = ref_measure.measure_circuits_deterministic(
            allow_sampling=True)
        targets = ref_measure.measure_counts_deterministic(shots)
        result = backend.run(circuits, shots=shots).result()
        self.assertSuccess(result)
        sampling = (method == "density_matrix")
        self.compare_result_metadata(result, circuits, "measure_sampling",
                                     sampling)