def main():
    import numpy as np
    n_qubit = 2
    obs = Observable(n_qubit)
    initial_state = QuantumState(n_qubit)
    obs.add_operator(1, "Z 0 Z 1")
    circuit_list = []
    p_list = [0.02, 0.04, 0.06, 0.08]

    #prepare circuit list
    for p in p_list:
        circuit = QuantumCircuit(n_qubit)
        circuit.add_H_gate(0)
        circuit.add_RY_gate(1, np.pi / 6)
        circuit.add_CNOT_gate(0, 1)
        circuit.add_gate(
            Probabilistic([p / 4, p / 4, p / 4],
                          [X(0), Y(0), Z(0)]))  #depolarizing noise
        circuit.add_gate(
            Probabilistic([p / 4, p / 4, p / 4],
                          [X(1), Y(1), Z(1)]))  #depolarizing noise
        circuit_list.append(circuit)

    #get mitigated output
    mitigated, non_mitigated_array, fit_coefs = error_mitigation_extrapolate_linear(
        circuit_list,
        p_list,
        initial_state,
        obs,
        n_circuit_sample=100000,
        return_full=True)

    #plot the result
    p = np.linspace(0, max(p_list), 100)
    plt.plot(p,
             fit_coefs[0] * p + fit_coefs[1],
             linestyle="--",
             label="linear fit")
    plt.scatter(p_list, non_mitigated_array, label="un-mitigated")
    plt.scatter(0, mitigated, label="mitigated output")

    #prepare the clean result
    state = QuantumState(n_qubit)
    circuit = QuantumCircuit(n_qubit)
    circuit.add_H_gate(0)
    circuit.add_RY_gate(1, np.pi / 6)
    circuit.add_CNOT_gate(0, 1)
    circuit.update_quantum_state(state)
    plt.scatter(0, obs.get_expectation_value(state), label="True output")
    plt.xlabel("error rate")
    plt.ylabel("expectation value")
    plt.legend()
    plt.show()
Exemple #2
0
 def test_add_same_gate_multiple_time(self):
     from qulacs import QuantumCircuit, QuantumState
     from qulacs.gate import X, DepolarizingNoise, DephasingNoise, Probabilistic, RX
     state = QuantumState(1)
     circuit = QuantumCircuit(1)
     noise = DepolarizingNoise(0, 0)
     circuit.add_gate(noise)
     circuit.add_gate(noise.copy())
     circuit.add_gate(DephasingNoise(0, 0))
     circuit.add_gate(Probabilistic([0.1], [RX(0, 0)]))
     gate = RX(0, 0)
     circuit.add_gate(gate)
     circuit.add_gate(gate)
     circuit.add_gate(gate)
     circuit.add_gate(gate)
     circuit.add_gate(gate)
     del gate
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.update_quantum_state(state)
     circuit.to_string()
     del circuit
     del state
Exemple #3
0
    def test_circuit_add_gate(self):
        from qulacs import QuantumCircuit, QuantumState
        from qulacs.gate import Identity, X, Y, Z, H, S, Sdag, T, Tdag, sqrtX, sqrtXdag, sqrtY, sqrtYdag
        from qulacs.gate import P0, P1, U1, U2, U3, RX, RY, RZ, CNOT, CZ, SWAP, TOFFOLI, FREDKIN, Pauli, PauliRotation
        from qulacs.gate import DenseMatrix, SparseMatrix, DiagonalMatrix, RandomUnitary, ReversibleBoolean, StateReflection
        from qulacs.gate import BitFlipNoise, DephasingNoise, IndependentXZNoise, DepolarizingNoise, TwoQubitDepolarizingNoise, AmplitudeDampingNoise, Measurement
        from qulacs.gate import merge, add, to_matrix_gate, Probabilistic, CPTP, Instrument, Adaptive
        from scipy.sparse import lil_matrix
        qc = QuantumCircuit(3)
        qs = QuantumState(3)
        ref = QuantumState(3)
        sparse_mat = lil_matrix((4, 4))
        sparse_mat[0, 0] = 1
        sparse_mat[1, 1] = 1

        def func(v, d):
            return (v + 1) % d

        def adap(v):
            return True

        gates = [
            Identity(0), X(0), Y(0), Z(0), H(0), S(0), Sdag(0), T(0), Tdag(0), sqrtX(0), sqrtXdag(0), sqrtY(0), sqrtYdag(0),
            Probabilistic([0.5, 0.5], [X(0), Y(0)]), CPTP([P0(0), P1(0)]), Instrument([P0(0), P1(0)], 1), Adaptive(X(0), adap),
            CNOT(0, 1), CZ(0, 1), SWAP(0, 1), TOFFOLI(0, 1, 2), FREDKIN(0, 1, 2), Pauli([0, 1], [1, 2]), PauliRotation([0, 1], [1, 2], 0.1),
            DenseMatrix(0, np.eye(2)), DenseMatrix([0, 1], np.eye(4)), SparseMatrix([0, 1], sparse_mat),
            DiagonalMatrix([0, 1], np.ones(4)), RandomUnitary([0, 1]), ReversibleBoolean([0, 1], func), StateReflection(ref),
            BitFlipNoise(0, 0.1), DephasingNoise(0, 0.1), IndependentXZNoise(0, 0.1), DepolarizingNoise(0, 0.1), TwoQubitDepolarizingNoise(0, 1, 0.1),
            AmplitudeDampingNoise(0, 0.1), Measurement(0, 1), merge(X(0), Y(1)), add(X(0), Y(1)), to_matrix_gate(X(0)),
            P0(0), P1(0), U1(0, 0.), U2(0, 0., 0.), U3(0, 0., 0., 0.), RX(0, 0.), RY(0, 0.), RZ(0, 0.),
        ]
        gates.append(merge(gates[0], gates[1]))
        gates.append(add(gates[0], gates[1]))

        ref = None
        for gate in gates:
            qc.add_gate(gate)

        for gate in gates:
            qc.add_gate(gate)

        qc.update_quantum_state(qs)
        qc = None
        qs = None
        for gate in gates:
            gate = None

        gates = None
        parametric_gates = None
Exemple #4
0
def test_VqeOptimizer():
    from qulacs import ParametricQuantumCircuit
    from qulacs import QuantumState
    from qulacs import Observable
    from qulacs.gate import Probabilistic, X, Y, Z
    import numpy as np
    import matplotlib.pyplot as plt

    n_qubit = 2
    p_list = [0.05, 0.1, 0.15]
    parametric_circuit_list = \
        [ParametricQuantumCircuit(n_qubit)
         for i in range(len(p_list))]
    initial_state = QuantumState(n_qubit)

    for (p, circuit) in zip(p_list, parametric_circuit_list):
        circuit.add_H_gate(0)
        circuit.add_parametric_RY_gate(1, np.pi / 6)
        circuit.add_CNOT_gate(0, 1)
        prob = Probabilistic([p / 4, p / 4, p / 4], [X(0), Y(0), Z(0)])
        circuit.add_gate(prob)

    noiseless_circuit = ParametricQuantumCircuit(n_qubit)
    noiseless_circuit.add_H_gate(0)
    noiseless_circuit.add_parametric_RY_gate(1, np.pi / 6)
    noiseless_circuit.add_CNOT_gate(0, 1)

    n_sample_per_circuit = 1
    n_circuit_sample = 1000
    obs = Observable(n_qubit)
    obs.add_operator(1.0, "Z 0 Z 1")
    obs.add_operator(0.5, "X 0 X 1")
    initial_param = np.array([np.pi / 6])

    opt = VqeOptimizer(parametric_circuit_list,
                       initial_state,
                       obs,
                       initial_param,
                       p_list,
                       n_circuit_sample=n_circuit_sample,
                       n_sample_per_circuit=n_sample_per_circuit,
                       noiseless_circuit=noiseless_circuit)

    noisy = opt.sample_output(initial_param)
    mitigated, exp_array, _ = opt.sample_mitigated_output(initial_param,
                                                          return_full=True)
    exact = opt.exact_output(initial_param)

    print(noisy, exact)
    print(exp_array, mitigated)

    opt_param = opt.optimize()
    print(opt_param)
    theta_list = np.linspace(0, np.pi, 100)
    output_list = [opt.exact_output([theta]) for theta in theta_list]
    plt.plot(theta_list,
             output_list,
             color="black",
             linestyle="dashed",
             label="exact")
    plt.scatter(opt.parameter_history,
                opt.exp_history,
                c="blue",
                label="optimization history")
    plt.xlabel("theta")
    plt.ylabel("output")
    plt.legend()
    plt.show()
Exemple #5
0
from qulacs import Observable
from qulacs import QuantumState, QuantumCircuit
from qulacs.gate import Probabilistic, X

import matplotlib.pyplot as plt

obs = Observable(1)
obs.add_operator(1, "Z 0")
state = QuantumState(1)
circuit = QuantumCircuit(1)
p = 0.1  # probability of bit flip
n_circuit_sample = 10000
n_depth = 20  # the number of probabilistic gate

probabilistic_pauli_gate = Probabilistic([p],
                                         [X(0)])  #define probabilistic gate

circuit.add_gate(probabilistic_pauli_gate)  # add the prob. gate to the circuit

exp_array = []
for depth in range(n_depth):
    exp = 0
    for i in [0] * n_circuit_sample:
        state.set_zero_state()
        for _ in range(depth):
            circuit.update_quantum_state(state)  # apply the prob. gate
        exp += obs.get_expectation_value(
            state)  # get expectation value for one sample of circuit
    exp /= n_circuit_sample  # get overall average
    exp_array.append(exp)