Exemplo n.º 1
0
def test_macro():
    def macro(c, i):
        return c.h[i]
    BlueqatGlobalSetting.register_macro('foo', macro)
    try:
        assert is_vec_same(Circuit().foo(1).run(), Circuit().h[1].run())
    finally:
        BlueqatGlobalSetting.unregister_macro('foo')
Exemplo n.º 2
0
def test_switch_backend1():
    c = Circuit().x[0].h[0]
    assert np.array_equal(c.run(), c.run(backend="numpy"))

    BlueqatGlobalSetting.set_default_backend("qasm_output")
    assert c.run() == c.to_qasm()

    # Different instance of QasmOutputBackend is used.
    # Lhs is owned by Circuit, rhs is passed as argument. But in this case same result.
    from blueqat.backends.qasm_output_backend import QasmOutputBackend
    assert c.run(output_prologue=False) == c.run(False, backend=QasmOutputBackend())

    BlueqatGlobalSetting.set_default_backend("numpy")
    assert c.run(shots=5) == c.run_with_numpy(shots=5)
Exemplo n.º 3
0
# Copyright 2019 The Blueqat Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from blueqat import BlueqatGlobalSetting

DEFAULT_BACKEND = BlueqatGlobalSetting.get_default_backend_name()


def pytest_addoption(parser):
    parser.addoption('--add-backend',
                     default=[DEFAULT_BACKEND],
                     action='append')


def pytest_generate_tests(metafunc):
    if 'backend' in metafunc.fixturenames:
        metafunc.parametrize('backend',
                             metafunc.config.getoption('--add-backend'))
Exemplo n.º 4
0
        return c.s[:].cz[0, 1].s[:]
    elif val == 0b01:
        return c.s[1].cz[0, 1].s[1]
    elif val == 0b10:
        return c.s[0].cz[0, 1].s[0]
    elif val == 0b11:
        return c.cz[0, 1]


def amplify(c):
    return c.h[:].x[:].cz[0, 1].x[:].h[:]


def grover(c, val):
    for _ in range(1):  # time of √N (N is qubits)
        c += Circuit().mark(val).amplify()
    return c


# BlueqatGlobalSetting.unregister_macro('mark')
# BlueqatGlobalSetting.unregister_macro('amplify')
# BlueqatGlobalSetting.unregister_macro('grover')
BlueqatGlobalSetting.register_macro('mark', mark)
BlueqatGlobalSetting.register_macro('amplify', amplify)
BlueqatGlobalSetting.register_macro('grover', grover)

# %% practice

result = Circuit(2).h[:].grover(0b01).m[:].run(shots=100)
print(result)
Exemplo n.º 5
0
def register_backend(name: str = 'braketconverter',
                     allow_overwrite: bool = False) -> None:
    """Register BraketConverterBackend to Blueqat."""
    BlueqatGlobalSetting.register_backend(name, BraketConverterBackend,
                                          allow_overwrite)
Exemplo n.º 6
0
from blueqat import BlueqatGlobalSetting

class HelloGate(Gate):
    lowername = "hello"
    def fallback(self, n_qubits):
        print("Hello")
        return []

BlueqatGlobalSetting.register_gate("hello", HelloGate)
BlueqatGlobalSetting.register_gate("yeah", HelloGate)
c = Circuit().hello[0].yeah[0]
c.run()
    :returns Vqe object
    """
    sampler = sampler or vqe.non_sampling_sampler
    minimizer = minimizer or vqe.get_scipy_minimizer(
        method="Powell",
        options={"ftol": 5.0e-2, "xtol": 5.0e-2, "maxiter": 1000, "disp": True}
    )
    hamiltonian = pauli.I() * 0

    for i, j in edges:
        hamiltonian += pauli.Z(i) * pauli.Z(j)

    return vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, n_step), minimizer, sampler)

if __name__ == "__main__":
    print("Input token:")
    token = input().strip()
    BlueqatGlobalSetting.set_default_backend("mqc")
    #sampler = vqe.get_measurement_sampler(1024)
    sampler = vqe.get_measurement_sampler(1024, {"token": token})

    runner = maxcut_qaoa(2, [(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2), (4, 0), (4, 3)], sampler=sampler)
    result = runner.run(verbose=True)
    print("""
       {4}
      / \\
     {0}---{3}
     | x |
     {1}---{2}
""".format(*result.most_common()[0][0]))