Esempio n. 1
0
 def test_is_cptp(self):
     """Test is_cptp method."""
     self.assertTrue(Kraus(self.depol_kraus(0.5)).is_cptp())
     self.assertTrue(Kraus(self.UX).is_cptp())
     # Non-CPTP should return false
     self.assertFalse(Kraus(([self.UI], [self.UX])).is_cptp())
     self.assertFalse(Kraus(([self.UI, self.UX])).is_cptp())
Esempio n. 2
0
    def test_init(self):
        """Test initialization"""
        # Initialize from unitary
        chan = Kraus(self.UI)
        assert_allclose(chan.data, [self.UI])
        self.assertEqual(chan.dim, (2, 2))

        # Initialize from Kraus
        chan = Kraus(self.depol_kraus(0.5))
        assert_allclose(chan.data, self.depol_kraus(0.5))
        self.assertEqual(chan.dim, (2, 2))

        # Initialize from Non-CPTP
        kraus_l, kraus_r = [self.UI, self.UX], [self.UY, self.UZ]
        chan = Kraus((kraus_l, kraus_r))
        assert_allclose(chan.data, (kraus_l, kraus_r))
        self.assertEqual(chan.dim, (2, 2))

        # Initialize with redundant second op
        chan = Kraus((kraus_l, kraus_l))
        assert_allclose(chan.data, kraus_l)
        self.assertEqual(chan.dim, (2, 2))

        # Initialize from rectangular
        kraus = [np.zeros((4, 2))]
        chan = Kraus(kraus)
        assert_allclose(chan.data, kraus)
        self.assertEqual(chan.dim, (2, 4))

        # Wrong input or output dims should raise exception
        self.assertRaises(QiskitError,
                          Kraus,
                          kraus,
                          input_dims=4,
                          output_dims=4)
Esempio n. 3
0
    def test_tensor(self):
        """Test tensor method."""
        rho0, rho1 = np.diag([1, 0]), np.diag([0, 1])
        rho_init = DensityMatrix(np.kron(rho0, rho0))
        chan1 = Kraus(self.UI)
        chan2 = Kraus(self.UX)

        # X \otimes I
        chan = chan2.tensor(chan1)
        rho_targ = DensityMatrix(np.kron(rho1, rho0))
        self.assertEqual(chan.dim, (4, 4))
        self.assertEqual(rho_init @ chan, rho_targ)

        # I \otimes X
        chan = chan1.tensor(chan2)
        rho_targ = DensityMatrix(np.kron(rho0, rho1))
        self.assertEqual(chan.dim, (4, 4))
        self.assertEqual(rho_init @ chan, rho_targ)

        # Completely depolarizing
        chan_dep = Kraus(self.depol_kraus(1))
        chan = chan_dep.tensor(chan_dep)
        rho_targ = DensityMatrix(np.diag([1, 1, 1, 1]) / 4)
        self.assertEqual(chan.dim, (4, 4))
        self.assertEqual(rho_init @ chan, rho_targ)
Esempio n. 4
0
 def test_copy(self):
     """Test copy method"""
     mat = np.eye(2)
     with self.subTest("Deep copy"):
         orig = Kraus(mat)
         cpy = orig.copy()
         cpy._data[0][0][0, 0] = 0.0
         self.assertFalse(cpy == orig)
     with self.subTest("Shallow copy"):
         orig = Kraus(mat)
         clone = copy.copy(orig)
         clone._data[0][0][0, 0] = 0.0
         self.assertTrue(clone == orig)
Esempio n. 5
0
    def test_power(self):
        """Test power method."""
        # 10% depolarizing channel
        rho = DensityMatrix(np.diag([1, 0]))
        p_id = 0.9
        chan = Kraus(self.depol_kraus(1 - p_id))

        # Compose 3 times
        p_id3 = p_id**3
        chan3 = chan.power(3)
        targ3a = rho @ chan @ chan @ chan
        self.assertEqual(rho @ chan3, targ3a)
        targ3b = rho @ Kraus(self.depol_kraus(1 - p_id3))
        self.assertEqual(rho @ chan3, targ3b)
Esempio n. 6
0
 def test_clone(self):
     """Test clone method"""
     mat = np.eye(4)
     orig = Kraus(mat)
     clone = copy.copy(orig)
     clone._data[0][0][0, 0] = 0.0
     self.assertTrue(clone == orig)
Esempio n. 7
0
 def test_multiply_except(self):
     """Test multiply method raises exceptions."""
     chan = Kraus(self.depol_kraus(1))
     self.assertRaises(QiskitError, chan._multiply, 's')
     self.assertRaises(QiskitError, chan.__rmul__, 's')
     self.assertRaises(QiskitError, chan._multiply, chan)
     self.assertRaises(QiskitError, chan.__rmul__, chan)
Esempio n. 8
0
    def test_subtract(self):
        """Test subtract method."""
        # Random input test state
        rho = DensityMatrix(self.rand_rho(2))
        kraus1, kraus2 = self.rand_kraus(2, 4, 4), self.rand_kraus(2, 4, 4)

        # Random Single-Kraus maps
        chan1 = Kraus(kraus1)
        chan2 = Kraus(kraus2)
        targ = (rho @ chan1) - (rho @ chan2)
        chan = chan1 - chan2
        self.assertEqual(rho @ chan, targ)

        # Random Single-Kraus maps
        chan = Kraus((kraus1, kraus2))
        targ = 0 * (rho @ chan)
        chan = chan - chan
        self.assertEqual(rho @ chan, targ)
Esempio n. 9
0
    def test_multiply(self):
        """Test multiply method."""
        # Random initial state and Kraus ops
        rho = DensityMatrix(self.rand_rho(2))
        val = 0.5
        kraus1, kraus2 = self.rand_kraus(2, 4, 4), self.rand_kraus(2, 4, 4)

        # Single Kraus set
        chan1 = Kraus(kraus1)
        targ = val * (rho & chan1)
        chan = chan1._multiply(val)
        self.assertEqual(rho & chan, targ)
        chan = val * chan1
        self.assertEqual(rho & chan, targ)
        targ = (rho & chan1) * val
        chan = chan1 * val
        self.assertEqual(rho & chan, targ)

        # Double Kraus set
        chan2 = Kraus((kraus1, kraus2))
        targ = val * (rho & chan2)
        chan = chan2._multiply(val)
        self.assertEqual(rho & chan, targ)
        chan = val * chan2
        self.assertEqual(rho & chan, targ)
Esempio n. 10
0
def get_kraus_matrices_from_ibm_noise_model(noise_model):
    """Gets the kraus operators from a pre defined noise model

    Args:
        noise_model (qiskit.providers.aer.noise.NoiseModel): Noise model for circuit
    
    Return
        dict_of_kraus_operators(dict): A dictionary labelled by keys which are the basis gates and values are the list of kraus operators

    """

    retrieved_quantum_error_dict = noise_model._default_quantum_errors
    dict_of_kraus_operators = { gate: Kraus(retrieved_quantum_error_dict[gate]).data for gate in retrieved_quantum_error_dict }
    return dict_of_kraus_operators 
Esempio n. 11
0
 def test_transpose(self):
     """Test transpose method."""
     kraus_l, kraus_r = self.rand_kraus(2, 4, 4), self.rand_kraus(2, 4, 4)
     # Single Kraus list
     targ = Kraus([np.transpose(k) for k in kraus_l])
     chan1 = Kraus(kraus_l)
     chan = chan1.transpose()
     self.assertEqual(chan, targ)
     self.assertEqual(chan.dim, (4, 2))
     # Double Kraus list
     targ = Kraus(([np.transpose(k)
                    for k in kraus_l], [np.transpose(k) for k in kraus_r]))
     chan1 = Kraus((kraus_l, kraus_r))
     chan = chan1.transpose()
     self.assertEqual(chan, targ)
     self.assertEqual(chan.dim, (4, 2))
Esempio n. 12
0
    def test_add(self):
        """Test add method."""
        # Random input test state
        rho = DensityMatrix(self.rand_rho(2))
        kraus1, kraus2 = self.rand_kraus(2, 4, 4), self.rand_kraus(2, 4, 4)

        # Random Single-Kraus maps
        chan1 = Kraus(kraus1)
        chan2 = Kraus(kraus2)
        targ = (rho @ chan1) + (rho @ chan2)
        chan = chan1._add(chan2)
        self.assertEqual(rho @ chan, targ)
        chan = chan1 + chan2
        self.assertEqual(rho @ chan, targ)

        # Random Single-Kraus maps
        chan = Kraus((kraus1, kraus2))
        targ = 2 * (rho @ chan)
        chan = chan._add(chan)
        self.assertEqual(rho @ chan, targ)
Esempio n. 13
0
    def test_sub_qargs(self):
        """Test sub method with qargs."""
        rho = DensityMatrix(self.rand_rho(8))
        kraus = self.rand_kraus(8, 8, 4)
        kraus0 = self.rand_kraus(2, 2, 4)

        op = Kraus(kraus)
        op0 = Kraus(kraus0)
        eye = Kraus(self.UI)

        with self.subTest(msg='qargs=[0]'):
            value = op - op0([0])
            target = op - eye.tensor(eye).tensor(op0)
            self.assertEqual(rho @ value, rho @ target)

        with self.subTest(msg='qargs=[1]'):
            value = op - op0([1])
            target = op - eye.tensor(op0).tensor(eye)
            self.assertEqual(rho @ value, rho @ target)

        with self.subTest(msg='qargs=[2]'):
            value = op - op0([2])
            target = op - op0.tensor(eye).tensor(eye)
            self.assertEqual(rho @ value, rho @ target)
Esempio n. 14
0
    def test_add_qargs(self):
        """Test add method with qargs."""
        rho = DensityMatrix(self.rand_rho(8))
        kraus = self.rand_kraus(8, 8, 4)
        kraus0 = self.rand_kraus(2, 2, 4)

        op = Kraus(kraus)
        op0 = Kraus(kraus0)
        eye = Kraus(self.UI)

        with self.subTest(msg="qargs=[0]"):
            value = op + op0([0])
            target = op + eye.tensor(eye).tensor(op0)
            self.assertEqual(rho & value, rho & target)

        with self.subTest(msg="qargs=[1]"):
            value = op + op0([1])
            target = op + eye.tensor(op0).tensor(eye)
            self.assertEqual(rho & value, rho & target)

        with self.subTest(msg="qargs=[2]"):
            value = op + op0([2])
            target = op + op0.tensor(eye).tensor(eye)
            self.assertEqual(rho & value, rho & target)
Esempio n. 15
0
import matplotlib.pyplot as plt
from qiskit import (QuantumCircuit, execute, Aer)
from qiskit.providers.aer.noise.errors.standard_errors import pauli_error
from qiskit.quantum_info import Operator, Kraus

simulator = Aer.get_backend('qasm_simulator')

circuit = QuantumCircuit(12, 4)

p_error = 0.3
bit_flip = pauli_error([('X', p_error), ('I', 1 - p_error)])
idn = Kraus(bit_flip)

p_error_f = 0.15
bit_flip_f = pauli_error([('X', p_error_f), ('I', 1 - p_error_f)])
idnf = Kraus(bit_flip_f)


def safe_id(i):
    for j in [4, 5, 6, 7, 8, 9, 10, 11]:
        circuit.append(idnf, [j])

    circuit.cx(0, 4)
    circuit.cx(0, 5)
    circuit.h(0)
    circuit.h(4)
    circuit.h(5)
    circuit.cx(0, 6)
    circuit.cx(0, 7)
    circuit.cx(4, 8)
    circuit.cx(4, 9)
# Plot QASM simulation data
plot_histogram([counts_noisy, counts_corrected],
               title='3-Qubit Error Correction $(P_{error} = ' +
               str(error_prob) + ')$',
               legend=['w/o code', 'with code'],
               figsize=(12, 9),
               bar_labels=False)
plt.subplots_adjust(left=0.15, right=0.72, top=0.9, bottom=0.15)
plt.show()

# Initialize fidelity simulation objects
job = execute(circ + qecc.encoder_ckt, backend=svsm)
init_state = DensityMatrix(job.result().get_statevector())
job = execute(qecc.syndrome_ckt, backend=unit)
syndrome_op = Kraus(job.result().get_unitary())

# Initialize fidelity simulation parameters
p_error = [0.05 * i for i in range(11)]
f1 = []
f2 = []

# Evolve initial state
for p in p_error:

    # Build noise channel
    bit_flip_channel = Kraus([[[0, np.sqrt(p)], [np.sqrt(p), 0]],
                              [[np.sqrt(1 - p), 0], [0, np.sqrt(1 - p)]]])
    bit_flip_channel = bit_flip_channel.tensor(bit_flip_channel).tensor(
        bit_flip_channel)
    bit_flip_channel = bit_flip_channel.expand(Kraus(np.eye(2))).expand(
Esempio n. 17
0
 def test_equal(self):
     """Test __eq__ method"""
     kraus = [self.rand_matrix(2, 2) for _ in range(2)]
     self.assertEqual(Kraus(kraus), Kraus(kraus))
Esempio n. 18
0
 def test_circuit_init(self):
     """Test initialization from a circuit."""
     circuit, target = self.simple_circuit_no_measure()
     op = Kraus(circuit)
     target = Kraus(target)
     self.assertEqual(op, target)
Esempio n. 19
0
 def test_negate(self):
     """Test negate method"""
     rho = DensityMatrix(np.diag([1, 0]))
     targ = DensityMatrix(np.diag([-0.5, -0.5]))
     chan = -Kraus(self.depol_kraus(1))
     self.assertEqual(rho @ chan, targ)
Esempio n. 20
0
    def test_dot(self):
        """Test dot method."""
        # Random input test state
        rho = DensityMatrix(self.rand_rho(2))

        # UnitaryChannel evolution
        chan1 = Kraus(self.UX)
        chan2 = Kraus(self.UY)
        targ = rho.evolve(Kraus(self.UZ))
        self.assertEqual(rho.evolve(chan1.dot(chan2)), targ)
        self.assertEqual(rho.evolve(chan1 * chan2), targ)

        # 50% depolarizing channel
        chan1 = Kraus(self.depol_kraus(0.5))
        targ = rho @ Kraus(self.depol_kraus(0.75))
        self.assertEqual(rho.evolve(chan1.dot(chan1)), targ)
        self.assertEqual(rho.evolve(chan1 * chan1), targ)

        # Compose different dimensions
        kraus1, kraus2 = self.rand_kraus(2, 4, 4), self.rand_kraus(4, 2, 4)
        chan1 = Kraus(kraus1)
        chan2 = Kraus(kraus2)
        targ = rho @ chan1 @ chan2
        self.assertEqual(rho.evolve(chan2.dot(chan1)), targ)
        self.assertEqual(rho.evolve(chan2 * chan1), targ)
Esempio n. 21
0
    def test_compose_front(self):
        """Test deprecated front compose method."""
        # Random input test state
        rho = DensityMatrix(self.rand_rho(2))

        # UnitaryChannel evolution
        chan1 = Kraus(self.UX)
        chan2 = Kraus(self.UY)
        chan = chan1.compose(chan2, front=True)
        targ = rho @ Kraus(self.UZ)
        self.assertEqual(rho @ chan, targ)

        # 50% depolarizing channel
        chan1 = Kraus(self.depol_kraus(0.5))
        chan = chan1.compose(chan1, front=True)
        targ = rho @ Kraus(self.depol_kraus(0.75))
        self.assertEqual(rho @ chan, targ)

        # Compose different dimensions
        kraus1, kraus2 = self.rand_kraus(2, 4, 4), self.rand_kraus(4, 2, 4)
        chan1 = Kraus(kraus1)
        chan2 = Kraus(kraus2)
        targ = rho @ chan1 @ chan2
        chan = chan2.compose(chan1, front=True)
        self.assertEqual(chan.dim, (2, 2))
        self.assertEqual(rho @ chan, targ)
Esempio n. 22
0
 def test_compose_except(self):
     """Test compose different dimension exception"""
     self.assertRaises(QiskitError,
                       Kraus(np.eye(2)).compose, Kraus(np.eye(4)))
     self.assertRaises(QiskitError, Kraus(np.eye(2)).compose, 2)
Esempio n. 23
0
 def test_power_except(self):
     """Test power method raises exceptions."""
     chan = Kraus(self.depol_kraus(0.9))
     # Non-integer power raises error
     self.assertRaises(QiskitError, chan.power, 0.5)