Esempio n. 1
0
def test_create_timed_model():
    basis = (bases.general(2), )

    class SampleModel(Model):
        dim = 2

        @Model.gate(duration=20)
        def rotate_y(self, qubit):
            return (
                self.wait(qubit, 10),
                ParametrizedOperation(lib.rotate_y, basis),
                self.wait(qubit, 10),
            )

        @Model.gate(duration='t_twoqubit')
        def cphase(self, qubit_static, qubit_fluxed):
            return (
                self.wait(qubit_static, 0.5 * self.p('t_twoqubit')),
                self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')),
                lib.cphase(pi).at(qubit_static, qubit_fluxed),
                self.wait(qubit_static, 0.5 * self.p('t_twoqubit')),
                self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')),
            )

        @Model.gate(duration=lambda qubit, setup: 600
                    if qubit == 'D0' else 400)
        def strange_duration_gate(self, qubit):
            return lib.rotate_y(pi)

        @staticmethod
        def _filter_wait_placeholders(operation):
            return Operation.from_sequence([
                unit for unit in operation.units()
                if not isinstance(unit.operation, WaitPlaceholder)
            ])

        def finalize(self, circuit, bases_in=None):
            return circuit.finalize([self._filter_wait_placeholders], bases_in)

    sample_setup = Setup("""
    setup:
    - t_twoqubit: 40
    """)

    m = SampleModel(sample_setup)
    cnot = m.rotate_y('D0', angle=0.5*pi) + m.cphase('D0', 'D1') + \
           m.rotate_y('D0', angle=-0.5*pi)
    cnot = m.finalize(cnot)

    assert cnot.operation.ptm(basis * 2, basis * 2) == approx(
        Operation.from_sequence(
            lib.rotate_y(0.5 * pi).at(0),
            lib.cphase(pi).at(0, 1),
            lib.rotate_y(-0.5 * pi).at(0),
        ).ptm(basis * 2, basis * 2))

    gate1 = m.strange_duration_gate('D0')
    assert gate1.duration == 600
    gate2 = m.strange_duration_gate('D1')
    assert gate2.duration == 400
Esempio n. 2
0
def test_create_untimed_model():
    basis = (bases.general(2), )

    class SampleModel(Model):
        dim = 2

        @Model.gate()
        def rotate_y(self, qubit):
            return (ParametrizedOperation(lib.rotate_y, basis), )

        @Model.gate()
        def cphase(self, qubit_static, qubit_fluxed):
            return (lib.cphase(pi).at(qubit_static, qubit_fluxed), )

    sample_setup = Setup("""
    setup: []
    """)

    m = SampleModel(sample_setup)
    cnot = m.rotate_y('D0', angle=0.5*pi) + m.cphase('D0', 'D1') + \
           m.rotate_y('D0', angle=-0.5*pi)

    assert cnot.finalize().operation.ptm(basis * 2, basis * 2) == approx(
        Operation.from_sequence(
            lib.rotate_y(0.5 * pi).at(0),
            lib.cphase(pi).at(0, 1),
            lib.rotate_y(-0.5 * pi).at(0),
        ).ptm(basis * 2, basis * 2))
Esempio n. 3
0
    def test_gate_create_no_params(self):
        angle = 1.0758
        dim = 2

        op_1q = lib.rotate_x(angle)
        op_2q = lib.cphase(angle)

        gate = Gate('qubit', dim, op_1q)
        assert c_op(gate) == op_1q
        assert gate.qubits == ('qubit', )
        assert len(gate.free_parameters) == 0

        gate = Gate('Q0', dim, op_1q)
        assert c_op(gate) == op_1q
        assert gate.qubits == ('Q0', )
        assert len(gate.free_parameters) == 0

        gate = Gate(('Q1', ), dim, op_1q)
        assert c_op(gate) == op_1q
        assert gate.qubits == ('Q1', )
        assert len(gate.free_parameters) == 0

        gate = Gate(('D', 'A'), dim, op_2q)
        assert c_op(gate) == op_2q
        assert gate.qubits == ('D', 'A')
        assert len(gate.free_parameters) == 0
Esempio n. 4
0
    def test_gate_create(self):
        dim = 2
        gate = Gate('Q0', dim, lib.rotate_y(0.5 * pi), 20.)
        assert gate.time_start == 0
        assert gate.time_end == 20.
        assert gate.duration == 20.

        gate = Gate(['Q0', 'Q1'], dim, lib.cphase(0.5 * pi), 40., 125.)
        assert gate.time_start == 125.
        assert gate.time_end == approx(165.)
        assert gate.duration == 40.

        gate.time_end = 90.
        assert gate.time_start == approx(50.)
        assert gate.time_end == 90.
        assert gate.duration == 40.

        gate1 = gate.shift(time_start=0.)
        assert gate.time_start == approx(50.)
        assert gate.time_end == 90.
        assert gate.duration == 40.
        assert gate1.time_start == 0.
        assert gate1.time_end == 40.
        assert gate1.duration == 40.

        gate1 = gate.shift(time_end=123.)
        assert gate.time_start == approx(50.)
        assert gate.time_end == 90.
        assert gate.duration == 40.
        assert gate1.time_start == approx(83.)
        assert gate1.time_end == 123.
        assert gate1.duration == 40.
Esempio n. 5
0
    def test_circuits_add(self):
        dim = 2
        orplus = lib.rotate_y(0.5 * pi)
        ocphase = lib.cphase(pi)
        orminus = lib.rotate_y(-0.5 * pi)
        grplus = Gate('Q0', dim, orplus)
        gcphase = Gate(('Q0', 'Q1'), dim, ocphase)
        grminus = Gate('Q0', dim, orminus)
        basis = (bases.general(2), ) * 2

        circuit = grplus + gcphase
        assert circuit.qubits == ['Q0', 'Q1']
        assert len(circuit.gates) == 2
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0),
                                    ocphase.at(0, 1)).ptm(basis, basis))

        circuit = circuit + grminus
        assert circuit.qubits == ['Q0', 'Q1']
        assert len(circuit.gates) == 3
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0), ocphase.at(0, 1),
                                    orminus.at(0)).ptm(basis, basis))

        circuit = grplus + (gcphase + grminus)
        assert circuit.qubits == ['Q0', 'Q1']
        assert len(circuit.gates) == 3
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0), ocphase.at(0, 1),
                                    orminus.at(0)).ptm(basis, basis))

        grplus = Gate('Q1', dim, orplus)
        grminus = Gate('Q1', dim, orminus)
        circuit = grplus + gcphase + grminus
        assert circuit.qubits == ['Q1', 'Q0']
        assert len(circuit.gates) == 3
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0), ocphase.at(0, 1),
                                    orminus.at(0)).ptm(basis, basis))

        basis = (basis[0], ) * 3

        grplus = Gate('Q2', dim, orplus)
        grminus = Gate('Q0', dim, orminus)
        circuit = grplus + gcphase + grminus
        assert circuit.qubits == ['Q2', 'Q0', 'Q1']
        assert len(circuit.gates) == 3
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0), ocphase.at(1, 2),
                                    orminus.at(1)).ptm(basis, basis))

        grplus = Gate('Q0', dim, orplus)
        grminus = Gate('Q2', dim, orminus)
        circuit = grplus + gcphase + grminus
        assert circuit.qubits == ['Q0', 'Q1', 'Q2']
        assert len(circuit.gates) == 3
        assert c_op(circuit).ptm(basis, basis) == approx(
            Operation.from_sequence(orplus.at(0), ocphase.at(0, 1),
                                    orminus.at(2)).ptm(basis, basis))
Esempio n. 6
0
 def cphase(self, qubit_static, qubit_fluxed):
     return (
         self.wait(qubit_static, 0.5 * self.p('t_twoqubit')),
         self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')),
         lib.cphase(pi).at(qubit_static, qubit_fluxed),
         self.wait(qubit_static, 0.5 * self.p('t_twoqubit')),
         self.wait(qubit_fluxed, 0.5 * self.p('t_twoqubit')),
     )
Esempio n. 7
0
    def test_chain_apply(self):
        b = (bases.general(2), ) * 3
        dm = random_hermitian_matrix(8, seed=93)
        pv1 = PauliVector.from_dm(dm, b)
        pv2 = PauliVector.from_dm(dm, b)

        # Some random gate sequence
        op_indices = [(lib2.rotate_x(np.pi / 2), (0, )),
                      (lib2.rotate_y(0.3333), (1, )), (lib2.cphase(), (0, 2)),
                      (lib2.cphase(), (1, 2)),
                      (lib2.rotate_x(-np.pi / 2), (0, ))]

        for op, indices in op_indices:
            op(pv1, *indices),

        circuit = Operation.from_sequence(*(op.at(*ix)
                                            for op, ix in op_indices))
        circuit(pv2, 0, 1, 2)
        assert np.all(pv1.to_pv() == pv2.to_pv())
Esempio n. 8
0
    def test_ptm(self):
        # Some random gate sequence
        op_indices = [(lib2.rotate_x(np.pi / 2), (0, )),
                      (lib2.rotate_y(0.3333), (1, )), (lib2.cphase(), (0, 2)),
                      (lib2.cphase(), (1, 2)),
                      (lib2.rotate_x(-np.pi / 2), (0, ))]
        circuit = Operation.from_sequence(*(op.at(*ix)
                                            for op, ix in op_indices))

        b = (bases.general(2), ) * 3
        ptm = circuit.ptm(b, b)
        assert isinstance(ptm, np.ndarray)

        op_3q = Operation.from_ptm(ptm, b)
        dm = random_hermitian_matrix(8, seed=93)
        state1 = PauliVector.from_dm(dm, b)
        state2 = PauliVector.from_dm(dm, b)

        circuit(state1, 0, 1, 2)
        op_3q(state2, 0, 1, 2)
        assert np.allclose(state1.to_pv(), state2.to_pv())
Esempio n. 9
0
    def test_time_aware_add_gate_no_init_time(self):
        dim = 2
        orplus = lib.rotate_y(0.5 * pi)
        ocphase = lib.cphase(pi)
        orminus = lib.rotate_y(-0.5 * pi)
        gate_q0 = Gate('Q0', dim, orplus, 20.)
        gate_2q = Gate(['Q0', 'Q1'], dim, ocphase, 40.)
        gate_q1 = Gate('Q1', dim, orminus, 30.)

        circuit = gate_q0 + gate_2q
        assert circuit.time_start == 0.
        assert circuit.time_end == approx(60.)
        assert circuit.duration == approx(60.)
        assert [gate.time_start for gate in circuit.gates] == approx([0., 20.])

        circuit = circuit + gate_q1
        assert circuit.time_start == 0.
        assert circuit.time_end == approx(90.)
        assert circuit.duration == approx(90.)
        assert [gate.time_start for gate in circuit.gates] == \
               approx([0., 20., 60.])

        circuit = circuit + gate_q0
        assert circuit.time_start == 0.
        assert circuit.time_end == approx(90.)
        assert circuit.duration == approx(90.)
        assert [gate.time_start for gate in circuit.gates] == \
               approx([0., 20., 60., 60.])

        circuit = gate_q1 + circuit
        assert circuit.time_start == 0.
        assert circuit.time_end == approx(100.)
        assert circuit.duration == approx(100.)
        assert [gate.time_start for gate in circuit.gates] == \
               approx([0., 10., 30., 70., 70.])

        circuit = circuit + (gate_q0 + gate_q1)
        assert circuit.time_start == 0.
        assert circuit.time_end == approx(130.)
        assert circuit.duration == approx(130.)
        assert [gate.time_start for gate in circuit.gates] == \
               approx([0., 10., 30., 70., 70., 100., 100.])
Esempio n. 10
0
 def cphase(self, qubit_static, qubit_fluxed):
     return (lib.cphase(pi).at(qubit_static, qubit_fluxed), )
Esempio n. 11
0
 def cnot_like(angle_cphase, angle_rotate):
     return Operation.from_sequence(
         lib.rotate_y(angle_rotate).at(1),
         lib.cphase(angle_cphase).at(0, 1),
         lib.rotate_y(-angle_rotate).at(1))