Example #1
0
 def test_build_gate_with_constant(self):
     gate_def = GateDefinition("g", [Parameter("p", None)])
     sexpr = ("circuit", ("let", "a", 1), ("gate", "g", "a"))
     exp_value = gate_def(Constant("a", 1))
     circuit = build(sexpr, inject_pulses={"g": gate_def})
     act_value = circuit.body.statements[0]
     self.assertEqual(exp_value, act_value)
Example #2
0
 def get_gate_definition(self, name, params):
     """Return an existing or create a new GateDefinition."""
     if name not in self.gate_definitions:
         gate_def = GateDefinition(name, params)
         self.gate_definitions[name] = gate_def
     else:
         gate_def = self.gate_definitions[name]
     return gate_def
Example #3
0
 def test_unnormalized_native_gates(self):
     """Test using native gates that are not a dictionary."""
     gate_def = GateDefinition("g", [Parameter("p", None)])
     sexpr = ("circuit", ("gate", "g", 0))
     exp_value = gate_def(0)
     circuit = build(sexpr, inject_pulses=[gate_def])
     act_value = circuit.body.statements[0]
     self.assertEqual(exp_value, act_value)
Example #4
0
 def test_build_native_gate(self):
     name = randomize.random_identifier()
     parameters = [Parameter("a", ParamType.INT), Parameter("b", ParamType.FLOAT)]
     gate_def = GateDefinition(name, parameters=parameters)
     native_gates = {name: gate_def}
     a = 5
     b = 1.234
     sexpr = ("gate", name, a, b)
     exp_value = gate_def(a, b)
     act_value = build(sexpr, native_gates)
     self.assertEqual(exp_value, act_value)
Example #5
0
 def test_build_macro_definition(self):
     gate_def = GateDefinition("foo", parameters=[Parameter("x", None)])
     sexpr = (
         "macro",
         "bar",
         "a",
         "b",
         ("sequential_block", ("gate", "foo", "a"), ("gate", "foo", "b")),
     )
     act_value = build(sexpr, inject_pulses={"foo": gate_def})
     exp_value = core.Macro(
         "bar", parameters=[Parameter("a", None), Parameter("b", None)]
     )
     exp_value.body.statements.append(
         core.GateStatement(gate_def, parameters={"x": Parameter("a", None)})
     )
     exp_value.body.statements.append(
         core.GateStatement(gate_def, parameters={"x": Parameter("b", None)})
     )
     self.assertEqual(exp_value, act_value)
Example #6
0
def make_random_gate_definition(name=None,
                                parameter_count=None,
                                parameter_types=None,
                                return_params=False):
    """Create a random gate definition."""
    if name is None:
        name = random_identifier()
    if parameter_types is None:
        if parameter_count is None:
            parameter_count = random_integer(lower=0, upper=16)
        allowed_types = VALID_GATE_ARG_TYPES + [ParamType.NONE]
        parameters = make_random_parameter_list(count=parameter_count,
                                                allowed_types=allowed_types)
    else:
        parameters = make_random_parameter_list(
            parameter_types=parameter_types)
    gatedef = GateDefinition(name, parameters=parameters)
    if not return_params:
        return gatedef
    else:
        return gatedef, name, parameters
Example #7
0
    def test_build_circuit(self):
        """Build a circuit with as many features as possible."""
        # We've already built a circuit elsewhere but this test tries to tie everything in together.
        gate_def = GateDefinition("g", [Parameter("p", None)])
        native_gates = {"g": gate_def}
        sexpr = (
            "circuit",
            ("register", "r", 7),
            ("map", "q", "r"),
            ("let", "x", 0),
            ("macro", "foo", "a", ("sequential_block", ("gate", "g", "a"))),
            ("gate", "foo", "x"),
            ("loop", 5, ("sequential_block", ("gate", "g", 3))),
            ("parallel_block", ("gate", "g", 0), ("gate", "g", 1)),
        )
        act_value = build(sexpr, inject_pulses=native_gates)

        r = Register("r", 7)
        q = Register("q", alias_from=r)
        x = Constant("x", 0)
        foo = core.Macro("foo", parameters=[Parameter("a", None)])
        foo.body.statements.append(gate_def(Parameter("a", None)))
        exp_value = core.Circuit(native_gates=native_gates)
        exp_value.registers[r.name] = r
        exp_value.registers[q.name] = q
        exp_value.constants[x.name] = x
        exp_value.macros[foo.name] = foo
        exp_value.body.statements.append(foo(x))
        loop_block = core.BlockStatement(statements=[gate_def(3),])
        loop = core.LoopStatement(5, loop_block)
        exp_value.body.statements.append(loop)
        parallel_block = core.BlockStatement(parallel=True)
        parallel_block.statements.append(gate_def(0))
        parallel_block.statements.append(gate_def(1))
        exp_value.body.statements.append(parallel_block)

        self.assertEqual(exp_value, act_value)