Exemplo n.º 1
0
    def test_simple_hadamard(self):
        N = 1
        H_d = sigmaz()
        H_c = [sigmax()]
        qc = QubitCircuit(N)
        qc.add_gate("SNOT", 0)

        # test load_circuit, with verbose info
        num_tslots = 10
        evo_time = 10
        test = OptPulseProcessor(N, H_d, H_c)
        tlist, coeffs = test.load_circuit(
            qc, num_tslots=num_tslots, evo_time=evo_time, verbose=True)

        # test run_state
        rho0 = qubit_states(1, [0])
        plus = (qubit_states(1, [0]) + qubit_states(1, [1])).unit()
        result = test.run_state(rho0)
        assert_allclose(fidelity(result.states[-1], plus), 1, rtol=1.0e-6)

        # test add/remove ctrl
        test.add_ctrl(sigmay())
        test.remove_ctrl(0)
        assert_(
            len(test.ctrls) == 1,
            msg="Method of remove_ctrl could be wrong.")
        assert_allclose(test.drift, H_d)
        assert_(
            sigmay() in test.ctrls,
            msg="Method of remove_ctrl could be wrong.")
Exemplo n.º 2
0
    def TestNoise(self):
        """
        Test for Processor with noise
        """
        # setup and fidelity without noise
        init_state = qubit_states(2, [0, 0, 0, 0])
        tlist = np.array([0., np.pi/2.])
        a = destroy(2)
        proc = Processor(N=2)
        proc.add_control(sigmax(), targets=1)
        proc.pulses[0].tlist = tlist
        proc.pulses[0].coeff = np.array([1])
        result = proc.run_state(init_state=init_state)
        assert_allclose(
            fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])),
            1, rtol=1.e-7)

        # decoherence noise
        dec_noise = DecoherenceNoise([0.25*a], targets=1)
        proc.add_noise(dec_noise)
        result = proc.run_state(init_state=init_state)
        assert_allclose(
            fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])),
            0.981852, rtol=1.e-3)

        # white random noise
        proc.noise = []
        white_noise = RandomNoise(0.2, np.random.normal, loc=0.1, scale=0.1)
        proc.add_noise(white_noise)
        result = proc.run_state(init_state=init_state)
Exemplo n.º 3
0
    def test_multi_qubits(self):
        """
        Test for multi-qubits system.
        """
        N = 3
        H_d = tensor([sigmaz()] * 3)
        H_c = []

        # test empty ctrls
        num_tslots = 30
        evo_time = 10
        test = OptPulseProcessor(N)
        test.add_drift(H_d, [0, 1, 2])
        test.add_control(tensor([sigmax(), sigmax()]), cyclic_permutation=True)
        # test periodically adding ctrls
        sx = sigmax()
        iden = identity(2)
        # print(test.ctrls)
        # print(Qobj(tensor([sx, iden, sx])))
        assert_(Qobj(tensor([sx, iden, sx])) in test.ctrls)
        assert_(Qobj(tensor([iden, sx, sx])) in test.ctrls)
        assert_(Qobj(tensor([sx, sx, iden])) in test.ctrls)
        test.add_control(sigmax(), cyclic_permutation=True)
        test.add_control(sigmay(), cyclic_permutation=True)

        # test pulse genration for cnot gate, with kwargs
        qc = [tensor([identity(2), cnot()])]
        test.load_circuit(qc,
                          num_tslots=num_tslots,
                          evo_time=evo_time,
                          min_fid_err=1.0e-6)
        rho0 = qubit_states(3, [1, 1, 1])
        rho1 = qubit_states(3, [1, 1, 0])
        result = test.run_state(rho0, options=Options(store_states=True))
        assert_(fidelity(result.states[-1], rho1) > 1 - 1.0e-6)
Exemplo n.º 4
0
    def test_simple_hadamard(self):
        """
        Test for optimizing a simple hadamard gate
        """
        N = 1
        H_d = sigmaz()
        H_c = sigmax()
        qc = QubitCircuit(N)
        qc.add_gate("SNOT", 0)

        # test load_circuit, with verbose info
        num_tslots = 10
        evo_time = 10
        test = OptPulseProcessor(N, drift=H_d)
        test.add_control(H_c, targets=0)
        tlist, coeffs = test.load_circuit(qc,
                                          num_tslots=num_tslots,
                                          evo_time=evo_time,
                                          verbose=True)

        # test run_state
        rho0 = qubit_states(1, [0])
        plus = (qubit_states(1, [0]) + qubit_states(1, [1])).unit()
        result = test.run_state(rho0)
        assert_allclose(fidelity(result.states[-1], plus), 1, rtol=1.0e-6)

        # test add/remove ctrl
        test.add_control(sigmay())
        test.remove_pulse(0)
        assert_(len(test.pulses) == 1,
                msg="Method of remove_pulse could be wrong.")
        assert_allclose(test.drift.drift_hamiltonians[0].qobj, H_d)
        assert_(sigmay() in test.ctrls,
                msg="Method of remove_pulse could be wrong.")
Exemplo n.º 5
0
 def TestChooseSolver(self):
     # setup and fidelity without noise
     init_state = qubit_states(2, [0, 0, 0, 0])
     tlist = np.array([0., np.pi/2.])
     a = destroy(2)
     proc = Processor(N=2)
     proc.add_control(sigmax(), targets=1)
     proc.pulses[0].tlist = tlist
     proc.pulses[0].coeff = np.array([1])
     result = proc.run_state(init_state=init_state, solver="mcsolve")
     assert_allclose(
         fidelity(result.states[-1], qubit_states(2, [0, 1, 0, 0])),
         1, rtol=1.e-7) 
Exemplo n.º 6
0
    def testQubitStates(self):
        """
        Tests the qubit_states function.
        """
        psi0_a = basis(2, 0)
        psi0_b = qubit_states()
        assert_(psi0_a == psi0_b)

        psi1_a = basis(2, 1)
        psi1_b = qubit_states(states=[1])
        assert_(psi1_a == psi1_b)

        psi01_a = tensor(psi0_a, psi1_a)
        psi01_b = qubit_states(N=2, states=[0, 1])
        assert_(psi01_a == psi01_b)
Exemplo n.º 7
0
    def testQubitStates(self):
        """
        Tests the qubit_states function.
        """
        psi0_a = basis(2, 0)
        psi0_b = qubit_states()
        assert_(psi0_a == psi0_b)

        psi1_a = basis(2, 1)
        psi1_b = qubit_states(states=[1])
        assert_(psi1_a == psi1_b)

        psi01_a = tensor(psi0_a, psi1_a)
        psi01_b = qubit_states(N=2, states=[0, 1])
        assert_(psi01_a == psi01_b)
Exemplo n.º 8
0
    def test_multi_qubits(self):
        N = 3
        H_d = tensor([sigmaz()]*3)
        H_c = []

        # test empty ctrls
        num_tslots = 30
        evo_time = 10
        test = OptPulseProcessor(N, H_d, H_c)
        test.add_ctrl(tensor([sigmax(), sigmax()]),
                      cyclic_permutation=True)

        # test periodically adding ctrls
        sx = sigmax()
        iden = identity(2)
        assert_(Qobj(tensor([sx, iden, sx])) in test.ctrls)
        assert_(Qobj(tensor([iden, sx, sx])) in test.ctrls)
        assert_(Qobj(tensor([sx, sx, iden])) in test.ctrls)
        test.add_ctrl(sigmax(), cyclic_permutation=True)
        test.add_ctrl(sigmay(), cyclic_permutation=True)

        # test pulse genration for cnot gate, with kwargs
        qc = [tensor([identity(2), cnot()])]
        test.load_circuit(qc, num_tslots=num_tslots,
                          evo_time=evo_time, min_fid_err=1.0e-6)
        rho0 = qubit_states(3, [1, 1, 1])
        rho1 = qubit_states(3, [1, 1, 0])
        result = test.run_state(
            rho0, options=Options(store_states=True))
        print(result.states[-1])
        print(rho1)
        assert_(fidelity(result.states[-1], rho1) > 1-1.0e-6)

        # test save and read coeffs
        test.save_coeff("qutip_test_multi_qubits.txt")
        test2 = OptPulseProcessor(N, H_d, H_c)
        test2.drift = test.drift
        test2.ctrls = test.ctrls
        test2.read_coeff("qutip_test_multi_qubits.txt")
        os.remove("qutip_test_multi_qubits.txt")
        assert_(np.max((test.coeffs-test2.coeffs)**2) < 1.0e-13)
        result = test2.run_state(rho0,)
        assert_(fidelity(result.states[-1], rho1) > 1-1.0e-6)
Exemplo n.º 9
0
    def TestNoise(self):
        """
        Test for Processor with noise
        """
        # setup and fidelity without noise
        rho0 = qubit_states(2, [0, 0, 0, 0])
        tlist = np.array([0., np.pi / 2.])
        a = destroy(2)
        proc = Processor(N=2)
        proc.tlist = tlist
        proc.coeffs = np.array([1]).reshape((1, 1))
        proc.add_ctrl(sigmax(), targets=1)
        result = proc.run_state(rho0=rho0)
        assert_allclose(fidelity(result.states[-1],
                                 qubit_states(2, [0, 1, 0, 0])),
                        1,
                        rtol=1.e-7)

        # decoherence noise
        dec_noise = DecoherenceNoise([0.25 * a], targets=1)
        proc.add_noise(dec_noise)
        result = proc.run_state(rho0=rho0)
        assert_allclose(fidelity(result.states[-1],
                                 qubit_states(2, [0, 1, 0, 0])),
                        0.981852,
                        rtol=1.e-3)

        # white noise with internal/external operators
        proc.noise = []
        white_noise = RandomNoise(loc=0.1, scale=0.1)
        proc.add_noise(white_noise)
        result = proc.run_state(rho0=rho0)

        proc.noise = []
        white_noise = RandomNoise(loc=0.1, scale=0.1, ops=sigmax(), targets=1)
        proc.add_noise(white_noise)
        result = proc.run_state(rho0=rho0)
Exemplo n.º 10
0
def hst_measurement(state: Qobj, qcircuit, sample_size=1):
    """Hilbert-Schmidt test"""
    N = len(state.dims[0])
    qc = QubitCircuit(N * 2)
    qc.add_circuit(qcircuit)

    if state.isket:
        statein = tensor(state, qubit_states(N))
    elif state.isbra:
        statein = tensor(state, qubit_states(N).dag())
    elif state.isoper:
        statein = tensor(state, ket2dm(qubit_states(N)))
    state_preps = statein.transform(
        gate_sequence_product(bell_prep(N, True).propagators()))
    state_out = state_preps.transform(gate_sequence_product(qc.propagators()))
    state_postps = state_out.transform(
        gate_sequence_product(bell_prep(N).propagators()))

    prob = com_measure(state_postps)
    hst = np.random.choice(4**N, sample_size, p=prob)
    mresult = [
        state_index_number(state_postps.dims[0], result) for result in hst
    ]
    return mresult  # raw output
from numpy import pi
from qutip import tensor
from qutip.operators import sigmax, identity
from qutip.qip.circuit import QubitCircuit
from qutip.qip.gates import hadamard_transform, cnot, snot, swap, \
    gate_sequence_product
from qutip.qip.qubits import qubit_states
from qutip.states import basis, fock

print(basis(N=4, n=0))
print("\n")

print(fock(N=4, n=0))
print("\n")

print(qubit_states(N=2, states=[0, 0]))
print("\n")

x = sigmax()
print(x)
print("\n")

ket_zero = qubit_states(N=1, states=[0])

print(ket_zero)
print("\n")

print(x * ket_zero)
print("\n")

print(tensor([identity(N=2), hadamard_transform(N=1)]))
Exemplo n.º 12
0
from qutip.tensor import tensor

# %% [markdown]
# Three samples that reduce the dimension of a quantum state are given

# %%  Generate random input
N = 4  # number of qubits
n = 2  # number of reduced qubits

# Random inputs (qubit state)
Input = rand_ket(2**N, dims=[[2] * N, [1] * N])
# Input2 = rand_ket(2**N,dims=[[2]*N,[1]*N])
# Input = 0.99*Input*Input.dag() + 0.01*Input2*Input2.dag()  # To add some inpurity

# Reference State
r_state = qubit_states(n)  # 0 state

# %% Reduce the dimension of a quantum state by bring one of the subsystem to 0 state
print("quantum dimensionality reduction")
vc1 = vcirc(N)

for L in np.arange(1, 5):  # number of ansatz
    vc1.add_ansatz(np.zeros(N * 3))  # add one layer
    t0 = time()
    x0 = np.zeros(N * 3 * L)  # init parameters

    res = circ_maximize(x0,
                        Input,
                        vc1,
                        fid_ref,
                        r_state, [2, 3],
import pytest

import numpy as np

from variational_circuit.measure import *
from variational_circuit.vcirc import *

from qutip.states import bell_state
from qutip.qip.qubits import qubit_states
from qutip.metrics import fidelity

zero_state = qubit_states(2)
zero_qubit = qubit_states(1)
bell = bell_state()

vc = vcirc(2)


def test_add_ansatz():
    vc.add_ansatz([0, 0, 0, 0, 0, 0])
    vc.add_ansatz([0, 0, 0, 0, 0, 0])

    assert vc.structures == [('regular', None, {}), ('regular', None, {})]
    assert (vc.ansatzes[0].para == np.array([0, 0, 0, 0, 0, 0])).all()
    assert (vc.ansatzes[1].para == np.array([0, 0, 0, 0, 0, 0])).all()


def test_update_circuit():
    vc.update_ansatzes([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
    assert (vc.ansatzes[0].para == np.array([1, 1, 1, 1, 1, 1])).all()
    assert (vc.ansatzes[1].para == np.array([1, 1, 1, 1, 1, 1])).all()