Example #1
0
    def test_zz_parity_compilation(self):
        b_full = bases.general(3)
        b0 = b_full.subbasis([0])
        b01 = b_full.subbasis([0, 1])
        b012 = b_full.subbasis([0, 1, 2])

        bases_in = (b01, b01, b0)
        bases_out = (b_full, b_full, b012)
        zz = Operation.from_sequence(
            lib3.rotate_x(-np.pi / 2).at(2),
            lib3.cphase(leakage_rate=0.1).at(0, 2),
            lib3.cphase(leakage_rate=0.25).at(2, 1),
            lib3.rotate_x(np.pi / 2).at(2),
            lib3.rotate_x(np.pi).at(0),
            lib3.rotate_x(np.pi).at(1))
        zz_ptm = zz.ptm(bases_in, bases_out)
        zzc = zz.compile(bases_in=bases_in, bases_out=bases_out)
        zzc_ptm = zzc.ptm(bases_in, bases_out)
        assert zz_ptm == approx(zzc_ptm)

        assert len(zzc.operations) == 2
        op1, ix1 = zzc.operations[0]
        op2, ix2 = zzc.operations[1]
        assert ix1 == (0, 2)
        assert ix2 == (1, 2)
        assert op1.bases_in[0] == bases_in[0]
        assert op2.bases_in[0] == bases_in[1]
        assert op1.bases_in[1] == bases_in[2]
        # Qubit 0 did not leak
        assert op1.bases_out[0] == bases_out[0].subbasis([0, 1, 3, 4])
        # Qubit 1 leaked
        assert op2.bases_out[0] == bases_out[1].subbasis([0, 1, 2, 6])
        # Qubit 2 is measured
        assert op2.bases_out[1] == bases_out[2]

        dm = random_hermitian_matrix(3**3, seed=85)
        pv1 = PauliVector.from_dm(dm, (b01, b01, b0))
        pv2 = PauliVector.from_dm(dm, (b01, b01, b0))

        zz(pv1, 0, 1, 2)
        zzc(pv2, 0, 1, 2)

        # Compiled version still needs to be projected, so we can't compare
        # Pauli vectors, so we can to check only DM diagonals.
        assert np.allclose(pv1.diagonal(), pv2.diagonal())
Example #2
0
    def test_zz_parity_compilation(self):
        b_full = bases.general(3)
        b0 = b_full.subbasis([0])
        b01 = b_full.subbasis([0, 1])
        b012 = b_full.subbasis([0, 1, 2])

        bases_in = (b01, b01, b0)
        bases_out = (b_full, b_full, b012)
        zz = Operation.from_sequence(
            lib3.rotate_x(-np.pi/2).at(2),
            lib3.cphase(leakage=0.1).at(0, 2),
            lib3.cphase(leakage=0.25).at(2, 1),
            lib3.rotate_x(np.pi/2).at(2),
            lib3.rotate_x(np.pi).at(0),
            lib3.rotate_x(np.pi).at(1)
        )
        zzc = zz.compile(bases_in=bases_in, bases_out=bases_out)

        assert len(zzc.operations) == 2
        op1, ix1 = zzc.operations[0]
        op2, ix2 = zzc.operations[1]
        assert ix1 == (0, 2)
        assert ix2 == (1, 2)
        assert op1.bases_in[0] == bases_in[0]
        assert op2.bases_in[0] == bases_in[1]
        assert op1.bases_in[1] == bases_in[2]
        # Qubit 0 did not leak
        assert op1.bases_out[0] == bases_out[0].subbasis([0, 1, 3, 4])
        # Qubit 1 leaked
        assert op2.bases_out[0] == bases_out[1].subbasis([0, 1, 2, 6])
        # Qubit 2 is measured
        assert op2.bases_out[1] == bases_out[2]

        dm = random_density_matrix(3**3, seed=85)
        state1 = State.from_dm(dm, (b01, b01, b0))
        state2 = State.from_dm(dm, (b01, b01, b0))

        zz(state1, 0, 1, 2)
        zzc(state2, 0, 1, 2)

        # Compiled version still needs to be projected, so we can't compare
        # Pauli vectors, so we can to check only DM diagonals.
        assert np.allclose(state1.diagonal(), state2.diagonal())
    def test_rotate_x(self):
        basis = (bases.general(3), )
        sys_bases = basis * 3
        dm = State(sys_bases)

        rotate90 = lib.rotate_x(0.5 * np.pi)
        rotate180 = lib.rotate_x(np.pi)
        rotate360 = lib.rotate_x(2 * np.pi)

        rotate90(dm, 1)
        rotate180(dm, 2)
        assert np.allclose(dm.meas_prob(0), (1, 0, 0))
        assert np.allclose(dm.meas_prob(1), (0.5, 0.5, 0))
        assert np.allclose(dm.meas_prob(2), (0, 1, 0))

        rotate180(dm, 1)
        assert np.allclose(dm.meas_prob(1), (0.5, 0.5, 0))

        rotate90(dm, 1)
        assert np.allclose(dm.meas_prob(1), (1, 0, 0))

        rotate360(dm, 0)
        assert np.allclose(dm.meas_prob(0), (1, 0, 0))
Example #4
0
    def test_chain_create(self):
        op1 = lib2.rotate_x()
        op2 = lib2.rotate_y()
        op3 = lib2.cnot()
        op_qutrit = lib3.rotate_x()

        circuit = Operation.from_sequence(op1.at(0), op2.at(0))
        assert circuit.num_qubits == 1
        assert len(circuit.operations) == 2

        circuit = Operation.from_sequence(op1.at(1), op2.at(0))
        assert circuit.num_qubits == 2
        assert len(circuit.operations) == 2

        with pytest.raises(ValueError, match=".* must form an ordered set .*"):
            Operation.from_sequence(op1.at(2), op2.at(0))

        with pytest.raises(ValueError, match=".* must form an ordered set .*"):
            Operation.from_sequence(op1.at(1), op2.at(2))

        with pytest.raises(ValueError, match="Hilbert dimensionality of op.*"):
            Operation.from_sequence(op1.at(0), op_qutrit.at(0))

        circuit3q = Operation.from_sequence(op1.at(0), op2.at(1), op3.at(0, 1),
                                            op1.at(1), op2.at(0), op3.at(0, 2))
        assert circuit3q.num_qubits == 3
        assert len(circuit3q.operations) == 6

        with pytest.raises(ValueError, match="Number of indices is not .*"):
            Operation.from_sequence(op1.at(0), op3.at(0))

        with pytest.raises(ValueError, match="Number of indices is not .*"):
            circuit3q.at(0, 1)

        circuit4q = Operation.from_sequence(op3.at(0, 2),
                                            circuit3q.at(1, 2, 3))
        assert len(circuit4q.operations) == 7
        assert circuit4q.operations[0].indices == (0, 2)
        for o1, o2 in zip(circuit4q.operations[1:], circuit3q.operations):
            assert np.all(np.array(o1.indices) == np.array(o2.indices) + 1)

        circuit4q = Operation.from_sequence(circuit3q.at(2, 0, 3),
                                            op3.at(0, 1), op2.at(1))
        assert len(circuit4q.operations) == 8
        assert circuit4q.operations[0].indices == (2, )
        assert circuit4q.operations[1].indices == (0, )
        assert circuit4q.operations[2].indices == (2, 0)

        Operation.from_sequence(circuit3q.at(0, 1, 2),
                                Operation.from_sequence(op1, op2).at(1))
Example #5
0
    def test_chain_compile_leaking(self):
        b = bases.general(3)
        chain0 = Operation.from_sequence(
            lib3.rotate_x(0.5*np.pi).at(2),
            lib3.cphase(leakage=0.1).at(0, 2),
            lib3.cphase(leakage=0.1).at(1, 2),
            lib3.rotate_x(-0.75*np.pi).at(2),
            lib3.rotate_x(0.25*np.pi).at(2),
        )
        b0 = b.subbasis([0])
        b01 = b.subbasis([0, 1])
        b0134 = b.subbasis([0, 1, 3, 4])
        chain1 = chain0.compile((b0, b0, b0134), (b, b, b))
        # Ancilla is not leaking here
        anc_basis = chain1.operations[1].operation.bases_out[1]
        for label in anc_basis.labels:
            assert '2' not in label

        chain2 = chain0.compile((b01, b01, b0134), (b, b, b))
        # Ancilla is leaking here
        anc_basis = chain2.operations[1].operation.bases_out[1]
        for label in '2', 'X20', 'Y20', 'X21', 'Y21':
            assert label in anc_basis.labels
    def test_rotate_x(self):
        basis = (bases.general(3),)
        sys_bases = basis * 3
        dm = State(sys_bases)

        rotate90 = lib.rotate_x(0.5*np.pi)
        rotate180 = lib.rotate_x(np.pi)
        rotate360 = lib.rotate_x(2*np.pi)

        rotate90(dm, 1)
        rotate180(dm, 2)
        assert np.allclose(dm.meas_prob(0), (1, 0, 0))
        assert np.allclose(dm.meas_prob(1), (0.5, 0.5, 0))
        assert np.allclose(dm.meas_prob(2), (0, 1, 0))

        rotate180(dm, 1)
        assert np.allclose(dm.meas_prob(1), (0.5, 0.5, 0))

        rotate90(dm, 1)
        assert np.allclose(dm.meas_prob(1), (1, 0, 0))

        rotate360(dm, 0)
        assert np.allclose(dm.meas_prob(0), (1, 0, 0))
Example #7
0
    def test_chain_create(self):
        op1 = lib2.rotate_x()
        op2 = lib2.rotate_y()
        op3 = lib2.cnot()
        op_qutrit = lib3.rotate_x()

        circuit = Operation.from_sequence(op1.at(0), op2.at(0))
        assert circuit.num_qubits == 1
        assert len(circuit.operations) == 2

        circuit = Operation.from_sequence(op1.at(1), op2.at(0))
        assert circuit.num_qubits == 2
        assert len(circuit.operations) == 2

        with pytest.raises(ValueError, match=".* must form an ordered set .*"):
            Operation.from_sequence(op1.at(2), op2.at(0))

        with pytest.raises(ValueError, match=".* must form an ordered set .*"):
            Operation.from_sequence(op1.at(1), op2.at(2))

        with pytest.raises(ValueError, match="Hilbert dimensionality of op.*"):
            Operation.from_sequence(op1.at(0), op_qutrit.at(0))

        circuit3q = Operation.from_sequence(op1.at(0), op2.at(1), op3.at(0, 1),
                          op1.at(1), op2.at(0), op3.at(0, 2))
        assert circuit3q.num_qubits == 3
        assert len(circuit3q.operations) == 6

        with pytest.raises(ValueError, match="Number of indices is not .*"):
            Operation.from_sequence(op1.at(0), op3.at(0))

        with pytest.raises(ValueError, match="Number of indices is not .*"):
            circuit3q.at(0, 1)

        circuit4q = Operation.from_sequence(op3.at(0, 2), circuit3q.at(1, 2, 3))
        assert len(circuit4q.operations) == 7
        assert circuit4q.operations[0].indices == (0, 2)
        for o1, o2 in zip(circuit4q.operations[1:], circuit3q.operations):
            assert np.all(np.array(o1.indices) == np.array(o2.indices) + 1)

        circuit4q = Operation.from_sequence(
            circuit3q.at(2, 0, 3), op3.at(0, 1), op2.at(1))
        assert len(circuit4q.operations) == 8
        assert circuit4q.operations[0].indices == (2,)
        assert circuit4q.operations[1].indices == (0,)
        assert circuit4q.operations[2].indices == (2, 0)