Esempio n. 1
0
 def get_probs(self, t1, t2):
     prog = new_circuit()
     prog = add_decoherence_noise(prog, T1=t1, T2=t2)
     #result = self.qvm.run_and_measure(prog, [0,1,2,3,4,5,6,7], trials=100000)
     #print(len(result))
     #print(result.count([0]*(2**3)) )
     print('Hola')
Esempio n. 2
0
def add_noise_to_program(prog,
                         T1=30e-6,
                         T2=30e-6,
                         gate_time_1q=50e-9,
                         gate_time_2q=150e-09,
                         ro_fidelity=0.95):
    """
    Add generic damping and dephasing noise to a program.

    .. warning::

        This function is deprecated. Please use :py:func:`add_decoherence_noise` instead.

    :param prog: A pyquil program consisting of I, RZ, CZ, and RX(+-pi/2) instructions
    :param Union[Dict[int,float],float] T1: The T1 amplitude damping time either globally or in a
        dictionary indexed by qubit id. By default, this is 30 us.
    :param Union[Dict[int,float],float] T2: The T2 dephasing time either globally or in a
        dictionary indexed by qubit id. By default, this is also 30 us.
    :param float gate_time_1q: The duration of the one-qubit gates, namely RX(+pi/2) and RX(-pi/2).
        By default, this is 50 ns.
    :param float gate_time_2q: The duration of the two-qubit gates, namely CZ.
        By default, this is 150 ns.
    :param Union[Dict[int,float],float] ro_fidelity: The readout assignment fidelity
        :math:`F = (p(0|0) + p(1|1))/2` either globally or in a dictionary indexed by qubit id.
    :return: A new program with noisy operators.
    """
    warnings.warn(
        "pyquil.kraus.add_noise_to_program is deprecated, please use "
        "pyquil.noise.add_decoherence_noise instead.", DeprecationWarning)
    return add_decoherence_noise(prog,
                                 T1=T1,
                                 T2=T2,
                                 gate_time_1q=gate_time_1q,
                                 gate_time_2q=gate_time_2q,
                                 ro_fidelity=ro_fidelity)
Esempio n. 3
0
def test_decoherence_noise():
    prog = Program(RX(np.pi / 2, 0), CZ(0, 1), RZ(np.pi, 0))
    gates = _get_program_gates(prog)
    m1 = _decoherence_noise_model(gates,
                                  T1=INFINITY,
                                  T2=INFINITY,
                                  ro_fidelity=1.)

    # with no readout error, assignment_probs = identity matrix
    assert np.allclose(m1.assignment_probs[0], np.eye(2))
    assert np.allclose(m1.assignment_probs[1], np.eye(2))
    for g in m1.gates:
        # with infinite coherence time all kraus maps should only have a single, unitary kraus op
        assert len(g.kraus_ops) == 1
        k0, = g.kraus_ops
        # check unitarity
        k0dk0 = k0.dot(k0.conjugate().transpose())
        assert np.allclose(k0dk0, np.eye(k0dk0.shape[0]))

    # verify that selective (by qubit) dephasing and readout infidelity is working
    m2 = _decoherence_noise_model(gates,
                                  T1=INFINITY,
                                  T2={0: 30e-6},
                                  ro_fidelity={
                                      0: .95,
                                      1: 1.0
                                  })
    assert np.allclose(m2.assignment_probs[0], [[.95, 0.05], [.05, .95]])
    assert np.allclose(m2.assignment_probs[1], np.eye(2))
    for g in m2.gates:
        if 0 in g.targets:
            # single dephasing (no damping) channel on qc 0, no noise on qc1 -> 2 Kraus ops
            assert len(g.kraus_ops) == 2
        else:
            assert len(g.kraus_ops) == 1

    # verify that combined T1 and T2 will lead to 4 outcome Kraus map.
    m3 = _decoherence_noise_model(gates, T1={0: 30e-6}, T2={0: 30e-6})
    for g in m3.gates:
        if 0 in g.targets:
            # damping (implies dephasing) channel on qc 0, no noise on qc1 -> 4 Kraus ops
            assert len(g.kraus_ops) == 4
        else:
            assert len(g.kraus_ops) == 1

    # verify that gate names are translated
    new_prog = apply_noise_model(prog, m3)
    new_gates = _get_program_gates(new_prog)

    # check that headers have been embedded
    headers = _noise_model_program_header(m3)
    assert all(
        (isinstance(i, Pragma) and i.command in ["ADD-KRAUS", "READOUT-POVM"])
        or isinstance(i, DefGate) for i in headers)
    assert headers.out() in new_prog.out()

    # verify that high-level add_decoherence_noise reproduces new_prog
    new_prog2 = add_decoherence_noise(prog, T1={0: 30e-6}, T2={0: 30e-6})
    assert new_prog == new_prog2
Esempio n. 4
0
 def probabilities2(self, angles, t1, t2):
     prog = new_circuit()
     prog = add_decoherence_noise(prog, T1=t1, T2=t2)
     wf = self.qvm.wavefunction(prog)
     wf = wf.amplitudes.reshape((-1, 1))
     probs = np.zeros_like(wf)
     for xx in range(2**self.n_qubits):
         probs[xx] = np.conj(wf[xx]) * wf[xx]
     return probs
Esempio n. 5
0
 def get_probs_grover(self, t1, t2):
     prog = new_circuit_grover3()
     prog = add_decoherence_noise(prog, T1=t1, T2=t2)
     #result = self.qvm.run_and_measure(prog, [0,1,2,3,4,5,6,7], trials=100000)
     #print(len(result))
     #print(result.count([0]*(2**3)) )
     wf = self.qvm.wavefunction(prog)
     wf = wf.amplitudes.reshape((-1, 1))
     probs = np.zeros_like(wf)
     for xx in range(2**self.n_qubits):
         probs[xx] = np.conj(wf[xx]) * wf[xx]
     print(probs[0][0].real)
     return probs[0][0].real
def run_grovers(qc: str,
                N: int,
                begin: str,
                end: str,
                rounds=-1,
                offsets=[1, 0],
                trials=20,
                nlevel=0):
    print("Using qvm: " + qc)

    ret = {}
    for i in range(N):
        ret[i] = []

    qvm = get_qc(qc, as_qvm=True)

    p = Program()
    ro = p.declare('ro', 'BIT', N)

    # Initialize Non-Ancillary QBits
    for i in range(N):
        p += H(i)

    # Main Loop Setup
    if rounds == -1:  # Default number of rounds
        rounds = int(np.pi / 4 * np.sqrt(2**N))
    print("Running for " + str(rounds) + " rounds")

    diff_def, diffusion_op = create_diffusion_op(N)
    p += diff_def
    oracle = make_shift_rows_oracle(begin, end, N, offsets)

    # Main Loop
    for r in range(rounds):
        p += oracle
        p += diffusion_op(*range(N))

    # compile
    p = qvm.compile(p)
    p = Program(p.program)
    p = add_decoherence_noise(p, nlevel * 3e-5, nlevel * 3e-5, 5e-8, 1.5e-7, 1)
    p.pop()  # Remoes HALT instruction
    for i in range(N):
        p += MEASURE(i, ro[i])
    for n in range(trials):
        results = qvm.run(p)[0]
        for j in range(N):
            ret[j].append(results[j])

    return ret
Esempio n. 7
0
def run_with_gate_time_sampling(cxn: QVMConnection,
                                programs: Iterable[Tuple[float, Program]],
                                program_modifier=None,
                                trials=20000):
    records = []
    base = 50e-9
    gate_times = np.array([1, 2, 3, 4]) * base

    for param, program in programs:
        program = program.copy()
        ro = program.declare('ro', 'BIT', 2)
        for gate_time in gate_times:
            noisy = add_decoherence_noise(program,
                                          gate_time_1q=gate_time,
                                          gate_time_2q=3 * gate_time).inst([
                                              MEASURE(0, ro[0]),
                                              MEASURE(1, ro[1]),
                                          ])

            if program_modifier:
                noisy = program_modifier(noisy)

            bitstring = np.array(cxn.run(noisy, [0, 1], trials))
            z0, z1 = np.mean(bitstring, axis=0)
            zz = 1 - (np.sum(bitstring, axis=1) % 2).mean() * 2

            f0, f1 = (trials - np.sum(bitstring, axis=0)) / trials
            ff = np.sum(np.sum(bitstring, axis=1) == 0) / trials

            record = {
                'z0': z0,
                'z1': z1,
                'zz': zz,
                'f0': f0,
                'f1': f1,
                'ff': ff,
                'param': param,
                'noise_param': gate_time,
            }
            records += [record]

    return records
Esempio n. 8
0
    def decoherence_noise_model(self):
        """
        Adds the Rigetti decoherence noise model to the circuit

        The default noise parameters
        - T1 = 30 us                T1           = 30e-6
        - T2 = 30 us                T2           = 30e-6
        - 1q gate time = 50 ns      gate_time_1q = 50e-9
        - 2q gate time = 150 ns     gate_time_2q = 150e-09
                                    ro_fidelity  = 0.95
        """
        [T1, T2, gate_time_1q, gate_time_2q, ro_fidelity] = self.noise_params

        self.circuit = add_decoherence_noise(self.circuit,
                                             T1=T1,
                                             T2=T2,
                                             gate_time_1q=gate_time_1q,
                                             gate_time_2q=gate_time_2q,
                                             ro_fidelity=ro_fidelity)

        return self.circuit
Esempio n. 9
0
def add_dd(program: Program):
    new_program = program.copy_everything_except_instructions()

    counts = [0, 0]
    for gate in program:
        try:
            if len(gate.qubits) > 1:
                if abs(counts[0] - counts[1]) >= 2:
                    min_ind = int(counts[0] > counts[1])
                    times = max(int(abs(counts[0] - counts[1]) / 4), 1)

                    p = add_decoherence_noise(
                        Program(get_dd_sec(min_ind) * times))

                    new_program.inst(p)
                counts = [0, 0]
            else:
                counts[gate.qubits[0].index] += 1
        except AttributeError:
            pass

        new_program.inst(gate)
    return new_program
Esempio n. 10
0
from pyquil.noise import add_decoherence_noise
records = []
for theta in thetas:
    for t1 in t1s:
        prog = get_compiled_prog(theta)
        noisy = add_decoherence_noise(prog, T1=t1).inst([
            MEASURE(0, 0),
            MEASURE(1, 1),
        ])
        bitstrings = np.array(cxn.run(noisy, [0, 1], 1000))
Esempio n. 11
0
def get_noisy_executable(qc: QuantumComputer, native_program: Program,
                         noise_param: float):
    noisy = add_decoherence_noise(native_program,
                                  gate_time_1q=noise_param,
                                  gate_time_2q=3 * noise_param)
    return qc.compiler.native_quil_to_executable(noisy)
Esempio n. 12
0
def runExperiments(t1, t2, ro_fidelity, q1time, q2time):
    global MSG_LENGTH
    NUM_TRIALS = 1
    total_retries = 0
    total_noise_fails = 0
    qc = get_qc("9q-square-qvm")
    for trial in range(NUM_TRIALS):

        # perform secret sharing procedure once per message bit
        for i in range(MSG_LENGTH):
            while (True):  # retry until success
                alice_q, bob_q, charlie_q, program = initial_setup()
                alice_measure_dir, program = alice(alice_q, program)
                bob_measure_dir, program = bob(bob_q, program)
                charlie_measure_dir, program = charlie(charlie_q, program)

                # run program with noise
                program = program.measure_all()
                program = qc.compiler.quil_to_native_quil(program)
                program = add_decoherence_noise(program,
                                                T1=t1,
                                                T2=t2,
                                                gate_time_1q=q1time,
                                                gate_time_2q=q2time,
                                                ro_fidelity=ro_fidelity)
                program.wrap_in_numshots_loop(1000)
                program = qc.compiler.native_quil_to_executable(program)
                results = qc.run(program)

                # check if measurements were in right direction
                should_abort = not check_directions(
                    alice_measure_dir, bob_measure_dir, charlie_measure_dir)
                if should_abort:
                    print(
                        "Abort! Measurements yielded no useful information. Retrying..."
                    )
                    total_retries += 1
                    continue

                for j in range(1000):
                    curr_results = results[j]
                    alice_measure_result = curr_results[0]
                    bob_measure_result = curr_results[1]
                    charlie_measure_result = curr_results[2]

                    joint_result = bob_and_charlie(bob_measure_result,
                                                   charlie_measure_result)
                    if joint_result == alice_measure_result:
                        print(
                            "Success! Bob and Charlie reconstructed one bit of the secret message."
                        )
                    else:
                        print(
                            "Failure! Bob and Charlie reconstructed an incorrect bit of the secret message."
                        )
                        total_noise_fails += 1
                break

    print("Total retries: %d" % total_retries)
    print("Total noise fails: %d" % total_noise_fails)
    return total_noise_fails