示例#1
0
    def test_detuned2(self):
        self.setUp()
        # parameters
        v = 'detuned2'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        # preferably cz's parallel, but not with x 3
        k.gate("cz", [0, 2])
        k.gate("cz", [1, 4])
        k.gate("x", [3])

        # likewise, while y 3, no cz on 0,2 or 1,4
        k.gate("y", [3])
        k.gate("cz", [2, 5])
        k.gate("cz", [4, 6])

        prog.add_kernel(k)
        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
示例#2
0
def first_example():
    output_dir = os.path.join(_curdir_, 'test_output')
    ql.set_option('output_dir', output_dir)

    # You can specify a config location, here we use a default config
    config_fn = os.path.join(_curdir_, 'spin_demo_2811.json')
    platform = ql.Platform("spin_qubit_demo", config_fn)
    sweep_points = [1, 2]
    num_qubits = 2
    p = ql.Program("program", platform, num_qubits)
    p.set_sweep_points(sweep_points, len(sweep_points))

    # populate kernel using default gates
    k = ql.Kernel("kernel", platform, num_qubits)
    #k.prepz(0)

#####

    k.rx90(0)  # 145ns,ch 4,cw 1
    k.ry90(0)  # 145ns,ch 4,cw 3
    k.mrx90(0) # 145ns,ch 4,cw 2
    k.mry90(0) # 145ns,ch 4,cw 4
    k.identity(0) # 125ns,ch 4,cw 5

    #k.mry90(1) # 145ns,ch 5,cw 4
    k.ry90(1) # 145ns,ch 5,cw 3

#####

    # add the kernel to the program
    p.add_kernel(k)

    # compile the program
    p.compile()
示例#3
0
    def test_edge(self):
        self.setUp()
        # parameters
        v = 'edge'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        # no dependency, only a conflict in edge resource between the first two czs
        k.gate("cz", [1, 4])
        k.gate("cz", [0, 3])
        k.gate("cz", [2, 5])

        prog.add_kernel(k)
        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
示例#4
0
    def test_qwg2(self):
        self.setUp()
        # parameters
        v = 'qwg2'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + "_" + scheduler
        kernel_name = "kernel_" + v + "_" + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        for j in range(7):
            k.gate("x", [j])
        k.gate("x", [0])
        k.gate("y", [1])
        k.gate("y", [2])
        k.gate("x", [3])
        k.gate("y", [4])
        k.gate("x", [5])
        k.gate("y", [6])
        for j in range(7):
            k.gate("y", [j])

        prog.add_kernel(k)
        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
示例#5
0
    def test_adriaan(self):
        self.setUp()
        # parameters
        v = 'adriaan'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        k.gate("prepz", [0])
        k.gate("prepz", [2])
        for _ in range(10):
            k.gate("x", [0])

        for _ in range(6):
            k.gate("rx90", [2])
        k.gate("measure", [2])
        k.gate("measure", [0])

        prog.add_kernel(k)
        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
示例#6
0
    def test_3_qubit(self):
        self.setUpClass()
        nqubits = 3
        sweep_points = [2]

        k = ql.Kernel("aKernel", platf, nqubits)

        # populate kernel
        k.prepz(0)
        k.prepz(1)
        k.prepz(2)
        k.toffoli(0, 1, 2)
        k.measure(2)

        p = ql.Program("3_qubit_program", platf, nqubits)
        p.set_sweep_points(sweep_points)

        p.add_kernel(k)  # add kernel to program

        ql.set_option('decompose_toffoli', 'no')
        p.compile()

        gold_fn = rootDir + '/golden/test_3_qubit.qasm'
        qasm_fn = os.path.join(output_dir,
                               p.name + '_initialqasmwriter_out.qasm')
        self.assertTrue(file_compare(qasm_fn, gold_fn))
示例#7
0
    def setUpClass(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')

        # TODO cleanup
        ql.set_option('scheduler', 'ASAP')
        ql.set_option('scheduler_post179', 'yes')

        ql.set_option('log_level', 'LOG_WARNING')
示例#8
0
 def test_config_exception_hardware(self):
     output_dir = os.path.join(curdir, 'test_output')
     ql.set_option('output_dir', output_dir)
     config_fn = os.path.join(curdir, 'test_cfg_cbox_broken02.json')
     try:
         platf = ql.Platform("starmon", config_fn)
         raise
     except:
         pass
示例#9
0
    def test_uniform_scheduler_2(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        config_fn = os.path.join(curdir, 'test_cfg_none_s7.json')
        platform = ql.Platform('starmon', config_fn)

        num_qubits = 7
        p = ql.Program('test_uniform_scheduler_2', platform, num_qubits, 0)
        k = ql.Kernel('kernel_2', platform, num_qubits, 0)

        # again big bundles with x gates
        # alternated with non-trivial cnot bundles;
        # these cnots were chosen to be mutually independent
        # so will be going all 3 in one bundle
        # the single independent x will be moved with it
        # but because the cnots take 4 cycles,
        # also a lot of empty cycles are created
        # which cannot be filled except by that
        # stray independent x

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [0, 2])
        k.gate("cnot", [6, 3])
        k.gate("cnot", [1, 4])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [2, 5])
        k.gate("cnot", [3, 1])
        k.gate("cnot", [4, 6])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [2, 0])
        k.gate("cnot", [3, 6])
        k.gate("cnot", [4, 1])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [5, 2])
        k.gate("cnot", [1, 3])
        k.gate("cnot", [6, 4])

        for j in range(7):
            k.gate("x", [j])

        p.add_kernel(k)
        p.compile()
示例#10
0
    def test_7(self):
        self.setUp()
        # parameters
        v = '7'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        k.gate("prepz", [0])
        k.gate("prepz", [1])
        k.gate("prepz", [2])
        k.gate("prepz", [3])
        k.gate("prepz", [4])
        k.gate("prepz", [5])
        k.gate("prepz", [6])
        # preps all end at same time, is the base of ASAP

        # rotations on q0, q2 and q5, mutually independent, also wrt resource use
        k.gate("h", [0])  # qubit 0 in qwg0 10 cycles rotations
        k.gate("t", [0])
        k.gate("h", [0])
        k.gate("t", [0])

        k.gate("h", [2])  # qubit 2 in qwg1 5 cycles rotations
        k.gate("t", [2])

        k.gate("h", [5])  # qubit 4 in qwg2 2 cycles rotations

        # measures all start at same time, is the horizon for ALAP
        k.gate("measure", [0])
        k.gate("measure", [1])
        k.gate("measure", [2])
        k.gate("measure", [3])
        k.gate("measure", [4])
        k.gate("measure", [5])
        k.gate("measure", [6])

        prog.add_kernel(k)

        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
def create_program(pname: str, platf_cfg: str, nregisters: int = 32):
    """
    Wrapper around the constructor of openQL "Program" class.

    Args:
        pname       (str) : Name of the program
        platf_cfg   (str) : location of the platform configuration used to
            construct the OpenQL Platform used.
        nregisters  (int) : the number of classical registers required in
            the program.

    In addition to instantiating the Program, this function
        - creates a Platform based on the "platf_cfg" filename.
        - Adds the platform as an attribute  "p.platf"
        - Adds the output_dir as an attribute "p.output_dir"

    """

    # create OpenQL Program object see https://openql.readthedocs.io/en/latest/api/Program.html
    if 1:  # FIXME: workaround for OpenQL 0.8.1.dev4 re-setting option
        ql.set_option('output_dir', output_dir)

    platf = Platform('OpenQL_Platform', platf_cfg)
    nqubits = platf.get_qubit_number()
    p = Program(pname, platf, nqubits, nregisters)

    # add information to the Program object (FIXME: better create new type, seems to duplicate qubit_count and creg_count)
    p.platf = platf
    p.output_dir = output_dir
    p.nqubits = platf.get_qubit_number()
    p.nregisters = nregisters

    # detect OpenQL backend ('eqasm_compiler') used by inspecting platf_cfg
    p.eqasm_compiler = ''
    with open(platf_cfg) as f:
        for line in f:
            if 'eqasm_compiler' in line:
                m = re.search('"eqasm_compiler" *: *"(.*?)"', line)
                p.eqasm_compiler = m.group(1)
                break
    if p.eqasm_compiler == '':
        logging.error(f"key 'eqasm_compiler' not found in file '{platf_cfg}'")

    # determine extension of generated file
    #if p.eqasm_compiler == 'eqasm_backend_cc':
    if 1:  # FIXME: workaround for OpenQL 0.8.1.dev4 resetting values
        ext = '.vq1asm'  # CC
    else:
        ext = '.qisa'  # CC-light, QCC

    # add filename to help finding the output files. NB: file is created by calling compile()
    p.filename = join(p.output_dir, p.name + ext)
    return p
示例#12
0
def _openql_compile(dir_path, config_path, n_qubits, unitary):
    ql.set_option('output_dir', str(dir_path))
    platform = ql.Platform(PLATFORM_STR, str(config_path))
    p = ql.Program(PROGRAM_STR, platform, n_qubits)
    k = ql.Kernel(KERNEL_STR, platform, n_qubits)
    u_mat = ql.Unitary(UNITARY_STR, unitary.flatten())
    u_mat.decompose()
    k.gate(u_mat, range(n_qubits))
    p.add_kernel(k)
    c = ql.Compiler(COMPILER_STR)
    c.add_pass_alias('Writer', WRITER_STR)
    c.set_pass_option(WRITER_STR, 'write_qasm_files', 'yes')
    c.compile(p)
示例#13
0
        def qasmify(params, wpp):
            # ql.set_option('log_level', 'LOG_DEBUG')
            curdir = os.path.dirname(__file__)
            output_dir = os.path.join(curdir, 'test_output')
            ql.set_option('output_dir', output_dir)
            config_fn = os.path.join(curdir, 'hardware_config_qx.json')
            c = ql.Compiler("testCompiler")
            c.add_pass_alias("Writer", "outputIR")
            c.set_pass_option("outputIR", "write_qasm_files", "yes")
            platf = ql.Platform("myPlatform", config_fn)
            name = "qaoa_run"
            p = ql.Program(name, platf, n_qubits)
            k = ql.Kernel ("qk", platf, n_qubits)   
            # De-parameterize pqasm
            a_id = 0
            a_ctr = 0
            c_ctr = 0
            for i in pqasm:
                # 1-qubit parametric gates
                if i[0] == 'rx' or i[0] == 'ry' or i[0] == 'rz':
                    k.gate(i[0], [i[1]], 0, coeffs[c_ctr]*params[a_id])
                    c_ctr += 1
                    a_ctr += 1
                    if a_ctr >= ang_nos[a_id]:
                        a_id += 1
                        a_ctr = 0
                # 1-qubit discrete gates
                elif i[0] == 'x' or i[0] == 'y' or i[0] == 'z' or i[0] == 'h':
                    k.gate(i[0], [i[1]])
                # 2-qubit discrete gates
                else:
                    k.gate(i[0], i[1])
            
            # Pre-rotation for Z-basis measurement
            tgt = n_qubits-1
            for pt in wpp:
                if pt == "X":
                    k.gate("ry", tgt, 0, 1.5708)
                elif pt == "Y":
                    k.gate("rx", tgt, 0, -1.5708)
                # else Z or Identity
                tgt -= 1

            # Measure all
            if shots > 0:
                for i in range(n_qubits):
                    k.measure(i)

            p.add_kernel(k)
            
            c.compile(p)            
示例#14
0
    def test_single_qubit_flux_manual01(self):
        ql.set_option('cz_mode', 'manual')
        config_fn = os.path.join(curdir, 'hardware_config_cc_light.json')
        platform = ql.Platform('seven_qubits_chip', config_fn)
        num_qubits = platform.get_qubit_number()

        p = ql.Program('test_single_qubit_flux_manual01', platform, num_qubits)
        k = ql.Kernel('aKernel', platform, num_qubits)

        k.gate('prepz', [0])
        k.gate('prepz', [2])
        k.gate('sqf', [2])
        p.add_kernel(k)
        p.compile()
示例#15
0
    def test_uniform_scheduler_3(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        config_fn = os.path.join(curdir, 'test_cfg_none_s7.json')
        platform = ql.Platform('starmon', config_fn)

        num_qubits = 7
        p = ql.Program('test_uniform_scheduler_3', platform, num_qubits, 0)
        k = ql.Kernel('kernel_3', platform, num_qubits, 0)

        # as with test 2 but now the cnots are not mutually independent anymore
        # this already creates smaller bundles but more of them

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [6, 3])
        k.gate("cnot", [0, 2])
        k.gate("cnot", [1, 3])
        k.gate("cnot", [1, 4])
        k.gate("cnot", [0, 3])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [2, 5])
        k.gate("cnot", [3, 1])
        k.gate("cnot", [2, 0])
        k.gate("cnot", [3, 6])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [4, 1])
        k.gate("cnot", [3, 0])
        k.gate("cnot", [4, 6])

        for j in range(7):
            k.gate("x", [j])
        k.gate("cnot", [3, 5])
        k.gate("cnot", [5, 2])
        k.gate("cnot", [6, 4])
        k.gate("cnot", [5, 3])

        for j in range(7):
            k.gate("x", [j])

        p.add_kernel(k)
        p.compile()
示例#16
0
    def test_decompose_toffoli(self):
        config_fn = os.path.join(curdir, 'test_cfg_none_simple.json')
        platform = ql.Platform('platform_none', config_fn)
        num_qubits = 4

        p = ql.Program('test_decompose_toffoli', platform, num_qubits)
        k = ql.Kernel('kernel1', platform, num_qubits)

        k.hadamard(2)
        k.toffoli(0, 1, 2)
        k.hadamard(2)

        p.add_kernel(k)
        ql.set_option('decompose_toffoli', 'NC')
        p.compile()
示例#17
0
    def test_angle(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        platform = ql.Platform(platform_name, config_fn)

        p = ql.Program('test_angle', platform, num_qubits, num_cregs)
        k = ql.Kernel('kernel_0', platform, num_qubits, num_cregs)

        k.gate("rx180", [6], 0, 1.2345)     # NB: Python interface lacks classical parameter

        p.add_kernel(k)
        p.compile()
示例#18
0
    def test_stateful_behavior(self):
        self.setUpClass()
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')

        config_fn = os.path.join(curdir, 'hardware_config_cc_light.json')
        platform = ql.Platform("myPlatform", config_fn)

        sweep_points = [1]
        nqubits = 3
        nregs = 3

        p = ql.Program("statelessProgram", platform, nqubits, nregs)
        p.set_sweep_points(sweep_points)
        k = ql.Kernel("aKernel", platform, nqubits, nregs)

        k.prepz(0)
        k.gate('rx180', [0])
        k.measure(0)

        rd = ql.CReg()
        rs1 = ql.CReg()
        rs2 = ql.CReg()

        k.classical(rs1, ql.Operation(3))
        k.classical(rs1, ql.Operation(4))
        k.classical(rd, ql.Operation(rs1, '+', rs2))

        p.add_kernel(k)

        NCOMPILES = 50
        QISA_fn = os.path.join(output_dir, p.name + '.qisa')
        for i in range(NCOMPILES):
            p.compile()
            self.setUpClass()
            QISA_fn_i = os.path.join(output_dir,
                                     p.name + '_' + str(i) + '.qisa')
            os.rename(QISA_fn, QISA_fn_i)

        for i in range(NCOMPILES - 1):
            QISA_fn_1 = os.path.join(output_dir,
                                     p.name + '_' + str(i) + '.qisa')
            QISA_fn_2 = os.path.join(output_dir,
                                     p.name + '_' + str(i + 1) + '.qisa')
            self.assertTrue(file_compare(QISA_fn_1, QISA_fn_2))
示例#19
0
    def test_cqasm_default_gates(self):

        ql.set_option('use_default_gates', 'yes')

        nqubits = 4

        k = ql.Kernel("aKernel", platf, nqubits)
        k.gate('prep_z', [0])
        k.gate('identity', [0])
        k.gate('hadamard', [0])
        k.gate('x', [0])
        k.gate('y', [0])
        k.gate('z', [0])
        k.gate('rx90', [0])
        k.gate('ry90', [0])
        k.gate('mrx90', [0])
        k.gate('mry90', [0])
        k.gate('s', [0])
        k.gate('sdag', [0])
        k.gate('t', [0])
        k.gate('tdag', [0])
        k.gate('rx', [0], 0, 0.15)
        k.gate('ry', [0], 0, 0.15)
        k.gate('rz', [0], 0, 0.15)
        k.gate('cnot', [0, 1])
        k.gate('cz', [0, 1])
        k.gate('toffoli', [0, 1, 2])
        k.gate('swap', [0, 1])
        k.gate('measure', [0])
        k.gate('measure', [1])

        sweep_points = [2]

        p = ql.Program('test_cqasm_default_gates', platf, nqubits)
        p.set_sweep_points(sweep_points)
        p.add_kernel(k)
        p.compile()

        qasm_files = []
        qasm_files.append(os.path.join(output_dir, p.name+'.qasm'))
        qasm_files.append(os.path.join(output_dir, p.name+'_scheduled.qasm'))

        for qasm_file in qasm_files:
            # print('assembling: {}'.format(qasm_file))
            assemble(qasm_file)
示例#20
0
    def test_cqasm_custom_gates(self):
        self.setUpClass() 
        ql.set_option('use_default_gates', 'no')

        nqubits = 4

        k = ql.Kernel("aKernel", platf, nqubits)
        k.gate('prep_z', [0])
        k.gate('i', [0])
        k.gate('h', [0])
        k.gate('x', [0])
        k.gate('y', [0])
        k.gate('z', [0])
        k.gate('x90', [0])
        k.gate('y90', [0])
        k.gate('mx90', [0])
        k.gate('my90', [0])
        k.gate('s', [0])
        k.gate('sdag', [0])
        k.gate('t', [0])
        k.gate('tdag', [0])
        k.gate('rx', [0], 0, 0.15)
        k.gate('ry', [0], 0, 0.15)
        k.gate('rz', [0], 0, 0.15)
        k.gate('cnot', [0, 1])
        k.gate('cz', [0, 1])
        k.gate('toffoli', [0, 1, 2])
        k.gate('swap', [0, 1])
        k.gate('measure', [0])
        k.gate('measure', [1])

        sweep_points = [2]

        p = ql.Program('test_cqasm_custom_gates', platf, nqubits)
        p.set_sweep_points(sweep_points)
        p.add_kernel(k)
        p.compile()

        qasm_files = []
        qasm_files.append(os.path.join(output_dir, p.name+'_initialqasmwriter_out.qasm'))
        qasm_files.append(os.path.join(output_dir, p.name+'_scheduledqasmwriter_out.qasm'))

        for qasm_file in qasm_files:
            print('assembling: {}'.format(qasm_file))
            assemble(qasm_file)
示例#21
0
    def test_1(self):
        self.setUp()
        # parameters
        v = '1'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        for j in range(7):
            k.gate("x", [j])

        # a list of all cnots that are ok in trivial mapping
        k.gate("cnot", [0, 2])
        k.gate("cnot", [0, 3])
        k.gate("cnot", [1, 3])
        k.gate("cnot", [1, 4])
        k.gate("cnot", [2, 0])
        k.gate("cnot", [2, 5])
        k.gate("cnot", [3, 0])
        k.gate("cnot", [3, 1])
        k.gate("cnot", [3, 5])
        k.gate("cnot", [3, 6])
        k.gate("cnot", [4, 1])
        k.gate("cnot", [4, 6])
        k.gate("cnot", [5, 2])
        k.gate("cnot", [5, 3])
        k.gate("cnot", [6, 3])
        k.gate("cnot", [6, 4])

        prog.add_kernel(k)

        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))
示例#22
0
def compile(p, quiet: bool = True):
    """
    Wrapper around OpenQL Program.compile() method.
    """
    if quiet:
        with suppress_stdout():
            p.compile()
    else:  # show warnings
        ql.set_option('log_level', 'LOG_WARNING')
        p.compile()

    # determine extension of generated file
    if p.eqasm_compiler=='eqasm_backend_cc':
        ext = '.vq1asm' # CC
    else:
        ext = '.qisa' # CC-light, QCC
    # attribute is added to program to help finding the output files
    p.filename = join(p.output_dir, p.name + ext)
    return p
示例#23
0
    def test_uniform_scheduler_0(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        platform = ql.Platform(platform_name, config_fn)

        p = ql.Program('test_uniform_scheduler_0', platform, num_qubits, num_cregs)
        k = ql.Kernel('kernel_0', platform, num_qubits, num_cregs)

        # a simple first test
        # the x gates serve to separate the cnot gates wrt dependences
        # this creates big bundles with 7 x gates
        # and small bundles with just a cnot
        # after uniform scheduling, one or more x gates
        # should have been moved next to the cnot
        # those will move that do not have operands that overlap those of the cnot

        for j in range(6, 18+1):
            k.gate("x", [j])
        k.wait(all_qubits, 0);

        k.gate("cnot", [6, 7])
        k.wait(all_qubits, 0);

        for j in range(6, 18+1):
            k.gate("x", [j])
        k.wait(all_qubits, 0);

        k.gate("cnot", [12, 13])
        k.wait(all_qubits, 0);

        for j in range(6, 18+1):
            k.gate("x", [j])
        k.wait(all_qubits, 0);

        k.gate("cnot", [10, 15])

        p.add_kernel(k)
        p.compile()
示例#24
0
    def test_qi_example(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        platform = ql.Platform(platform_name, os.path.join(curdir, 'cc_s5_direct_iq.json'))

        p = ql.Program('test_qi_example', platform, 5, num_cregs)
        k = ql.Kernel('kernel_0', platform, 5, num_cregs)

        k.gate("prepz", [0, 1, 2, 3, 4])
        k.gate("ry180", [0, 2])     # FIXME: "y" does not work, but gate decomposition should handle?
        k.gate("cz", [0, 2])
        k.gate("y90", [2])
        k.gate("measure", [0, 1, 2, 3, 4])

        p.add_kernel(k)
        p.compile()
示例#25
0
    def test_gate_decomposition_builtin_gates(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_DEBUG')
        #ql.set_option('log_level', 'LOG_WARNING')

        platform = ql.Platform(platform_name, os.path.join(curdir, 'cc_s5_direct_iq.json'))

        p = ql.Program('test_gate_decomposition_builtin_gates', platform, 5, num_cregs)
        k = ql.Kernel('kernel_0', platform, 5, num_cregs)

        k.gate("cz", [0, 2])
        k.gate("cz", [2, 3])
        k.gate("cz", [3, 2])
        k.gate("cz", [2, 4])
        k.gate("cz", [4, 2])

        p.add_kernel(k)
        p.compile()
示例#26
0
    def test_uniform_scheduler_0(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        config_fn = os.path.join(curdir, 'test_cfg_none_s7.json')
        platform = ql.Platform('starmon', config_fn)

        num_qubits = 7
        p = ql.Program('test_uniform_scheduler_0', platform, num_qubits, 0)
        k = ql.Kernel('kernel_0', platform, num_qubits, 0)

        # a simple first test
        # the x gates serve to separate the cnot gates wrt dependences
        # this creates big bundles with 7 x gates
        # and small bundles with just a cnot
        # after uniform scheduling, one or more x gates
        # should have been moved next to the cnot
        # those will move that do not have operands that overlap those of the cnot

        for j in range(7):
            k.gate("x", [j])

        k.gate("cnot", [0, 2])

        for j in range(7):
            k.gate("x", [j])

        k.gate("cnot", [6, 3])

        for j in range(7):
            k.gate("x", [j])

        k.gate("cnot", [1, 4])

        p.add_kernel(k)
        p.compile()
示例#27
0
    def test_uniform_scheduler_4(self):
        ql.set_option('output_dir', output_dir)
        ql.set_option('optimize', 'no')
        ql.set_option('scheduler', 'ALAP')
        ql.set_option('scheduler_uniform', 'yes')
        ql.set_option('log_level', 'LOG_WARNING')

        config_fn = os.path.join(curdir, 'test_cfg_none_s7.json')
        platform = ql.Platform('starmon', config_fn)

        num_qubits = 7
        p = ql.Program('test_uniform_scheduler_4', platform, num_qubits, 0)
        k = ql.Kernel('kernel_4', platform, num_qubits, 0)

        # as with test 3 but now without the big x bundles
        # just the cnots in lexicographic order
        # the worst you can imagine,
        # creating the smallest bundles

        k.gate("cnot", [0, 2])
        k.gate("cnot", [0, 3])
        k.gate("cnot", [1, 3])
        k.gate("cnot", [1, 4])
        k.gate("cnot", [2, 0])
        k.gate("cnot", [2, 5])
        k.gate("cnot", [3, 0])
        k.gate("cnot", [3, 1])
        k.gate("cnot", [3, 5])
        k.gate("cnot", [3, 6])
        k.gate("cnot", [4, 1])
        k.gate("cnot", [4, 6])
        k.gate("cnot", [5, 2])
        k.gate("cnot", [5, 3])
        k.gate("cnot", [6, 3])
        k.gate("cnot", [6, 4])

        p.add_kernel(k)
        p.compile()
示例#28
0
def unit_tests():

    curdir = os.path.dirname(__file__)
    output_dir = os.path.join(curdir, 'qasm')

    ql.set_option('output_dir', output_dir)
    ql.set_option('write_qasm_files', 'yes')

    config_fn  = os.path.join(curdir, 'config_qx.json')
    platform   = ql.Platform('platform_none', config_fn)

    move = [0]          # Addend: 1-bit Move
    head = [1,2,3,4]    # Augend: 4-bit Head 
    anc = [5,6,7]       # Carry: uncomputed ancilla
    test = [8,9,10,11]

    circ_width = len(move) + len(head) + len(anc) + len(test)

    p = ql.Program('aritra', platform, circ_width)
    k_move = ql.Kernel("move", platform, circ_width)

    # Test using full superposition of head, both inc/dec and association to initial state
    for i in range(0,len(head)):
        k_move.gate('h',[head[i]])
        k_move.gate('cnot',[head[i],test[i]]) 
    k_move.gate('h', [move[0]])

    U_move(k_move, move, head, anc) 

    p.add_kernel(k_move)
    p.compile()
    print(p.qasm())
    qx = qxelarator.QX()
    qx.set(output_dir+'/aritra.qasm')
    qx.execute()
    isv = qx.get_state()
    print(isv)
示例#29
0
def grover_algorithm():
    ql.set_option('output_dir', output_dir)
    ql.set_option('optimize', 'no')
    ql.set_option('scheduler', 'ASAP')
    ql.set_option('log_level', 'LOG_CRITICAL')

    config_fn = os.path.join(curdir, 'hardware_config_qx.json')
    platform = ql.Platform('platform_none', config_fn)
    num_qubits = 9
    # oracle qubit
    oracle = 4

    # create a grover program
    p = ql.Program('test_grover', num_qubits, platform)

    # kernels
    init_k = init(platform)
    grover_k = grover(platform)
    result_k = ql.Kernel('result', platform)

    # result
    result_k.hadamard(oracle)
    result_k.measure(oracle)
    result_k.gate("display", [])

    result_k.measure(0)
    result_k.measure(1)
    result_k.measure(2)
    result_k.measure(3)
    result_k.gate("display_binary", [])

    # build the program
    p.add_kernel(init_k)
    p.add_kernel(grover_k, 3)
    p.add_kernel(result_k)
    ql.set_option('decompose_toffoli', 'NC')
    p.compile()
示例#30
0
    def test_issue179(self):
        self.setUp()
        # parameters
        v = 'issue179'
        scheduler = self._SCHEDULER

        # create and set platform
        prog_name = "test_" + v + '_' + scheduler
        kernel_name = "kernel_" + v + '_' + scheduler

        starmon = ql.Platform("starmon", self.config)
        prog = ql.Program(prog_name, starmon, 7, 0)
        k = ql.Kernel(kernel_name, starmon, 7, 0)

        # independent gates but stacking qwg unit use
        # in s7, q2, q3 and q4 all use qwg1
        # the y q3 must be in an other cycle than the x's because x conflicts with y in qwg1
        # the x q2 and x q4 can be in parallel but the y q3 in between prohibits this
        # because the qwg1 resource in single dimensional:
        # after x q2 it is busy on x in cycle 0,
        # then it only looks at the y q3, which requires to go to cycle 1,
        # and then the x q4 only looks at the current cycle (cycle 1),
        # in which qwg1 is busy with the y, so for the x it is busy,
        # and the only option is to go for cycle 2
        k.gate("x", [2])
        k.gate("y", [3])
        k.gate("x", [4])

        prog.add_kernel(k)
        ql.set_option("scheduler", scheduler)
        prog.compile()

        GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa')
        QISA_fn = os.path.join(output_dir, prog.name + '.qisa')

        assemble(QISA_fn)
        self.assertTrue(file_compare(QISA_fn, GOLD_fn))