Esempio n. 1
0
    def write_memory(self, *, region_name: str, offset: int = 0, value=None):
        """
        Writes a value into a memory region on the QAM at a specified offset.

        :param region_name: Name of the declared memory region on the QAM.
        :param offset: Integer offset into the memory region to write to.
        :param value: Value to store at the indicated location.
        """
        assert self.status in ['loaded', 'done']

        aref = ParameterAref(name=region_name, index=offset)
        self._variables_shim[aref] = value

        return self
Esempio n. 2
0
def test_rewrite_arithmetic_mixed():
    prog = Program(
        "DECLARE theta REAL",
        "DECLARE beta REAL",
        "RZ(3 * theta) 0",
        "RZ(beta+theta) 0",
    )
    response = rewrite_arithmetic(prog)
    assert response.original_memory_descriptors == {
        "theta": ParameterSpec(length=1, type="REAL"),
        "beta": ParameterSpec(length=1, type="REAL"),
    }
    assert response.recalculation_table == {
        ParameterAref(index=0, name="__P2"): "3*theta[0]/(2*pi)",
        ParameterAref(index=1, name="__P2"): "(beta[0] + theta[0])/(2*pi)",
    }
    assert (response.quil == Program(
        "DECLARE __P2 REAL[2]",
        "DECLARE theta REAL[1]",
        "DECLARE beta REAL[1]",
        "RZ(__P2[0]) 0",
        "RZ(__P2[1]) 0",
    ).out())
Esempio n. 3
0
def test_rewrite_arithmetic_duplicate_exprs():
    prog = Program(
        "DECLARE theta REAL",
        "RZ(theta*1.5) 0",
        "RX(theta*1.5) 0",  # this is not a native gate, but it is a protoquil program
    )

    response = rewrite_arithmetic(prog)

    assert response == RewriteArithmeticResponse(
        original_memory_descriptors={
            "theta": ParameterSpec(length=1, type="REAL")
        },
        recalculation_table={
            ParameterAref(index=0, name="__P1"): "theta[0]*1.5/(2*pi)"
        },
        quil=Program("DECLARE __P1 REAL[1]", "DECLARE theta REAL[1]",
                     "RZ(__P1[0]) 0", "RX(__P1[0]) 0").out(),
    )
def test_reset(client_configuration: QCSClientConfiguration):
    quantum_processor = NxQuantumProcessor(nx.complete_graph(3))
    qc = QuantumComputer(
        name="testy!",
        qam=QVM(client_configuration=client_configuration),
        compiler=DummyCompiler(quantum_processor=quantum_processor,
                               client_configuration=client_configuration),
    )
    p = Program(
        Declare(name="theta", memory_type="REAL"),
        Declare(name="ro", memory_type="BIT"),
        RX(MemoryReference("theta"), 0),
        MEASURE(0, MemoryReference("ro")),
    ).wrap_in_numshots_loop(10)
    p.write_memory(region_name="theta", value=np.pi)
    result = qc.qam.run(p)

    aref = ParameterAref(name="theta", index=0)
    assert p._memory.values[aref] == np.pi
    assert result.readout_data["ro"].shape == (10, 1)
    assert all([bit == 1 for bit in result.readout_data["ro"]])
Esempio n. 5
0
    def _resolve_memory_references(self, expression: Expression) -> Union[float, int]:
        """
        Traverse the given Expression, and replace any Memory References with whatever values
        have been so far provided by the user for those memory spaces. Declared memory defaults
        to zero.

        :param expression: an Expression
        """
        if isinstance(expression, BinaryExp):
            left = self._resolve_memory_references(expression.op1)
            right = self._resolve_memory_references(expression.op2)
            return expression.fn(left, right)
        elif isinstance(expression, Function):
            return expression.fn(self._resolve_memory_references(expression.expression))
        elif isinstance(expression, Parameter):
            raise ValueError(f"Unexpected Parameter in gate expression: {expression}")
        elif isinstance(expression, float) or isinstance(expression, int):
            return expression
        elif isinstance(expression, MemoryReference):
            return self._variables_shim.get(ParameterAref(name=expression.name, index=expression.offset), 0)
        else:
            raise ValueError(f"Unexpected expression in gate parameter: {expression}")
Esempio n. 6
0
def test_rewrite_arithmetic_set_scale():
    prog = Program(
        "DECLARE theta REAL",
        'SET-SCALE 0 "rf" 1.0',
        'SET-SCALE 0 "rf" theta',
    )

    response = rewrite_arithmetic(prog)

    assert response == RewriteArithmeticResponse(
        original_memory_descriptors={
            "theta": ParameterSpec(length=1, type="REAL")
        },
        recalculation_table={
            ParameterAref(index=0, name="__P1"): "theta[0]/8"
        },
        quil=Program(
            "DECLARE __P1 REAL[1]",
            "DECLARE theta REAL[1]",
            'SET-SCALE 0 "rf" 1.0',
            'SET-SCALE 0 "rf" __P1[0]',
        ).out(),
    )
Esempio n. 7
0
 def aref(ref: MemoryReference) -> ParameterAref:
     return ParameterAref(name=ref.name, index=ref.offset)