예제 #1
0
def _family(identifier_prefix: str,
            gate_maker: Callable[[int], 'cirq.Gate']) -> Iterator[CellMaker]:
    f = lambda args: ExplicitOperationsCell(
        [gate_maker(len(args.qubits)).on(*args.qubits)])
    yield CellMaker(identifier_prefix, 1, f)
    for i in CELL_SIZES:
        yield CellMaker(identifier_prefix + str(i), i, f)
예제 #2
0
def generate_all_input_cell_makers() -> Iterator[CellMaker]:
    # Quantum inputs.
    yield from _input_family("inputA", "a")
    yield from _input_family("inputB", "b")
    yield from _input_family("inputR", "r")
    yield from _input_family("revinputA", "a", rev=True)
    yield from _input_family("revinputB", "b", rev=True)

    # Classical inputs.
    yield CellMaker("setA", 2,
                    lambda args: SetDefaultInputCell('a', args.value))
    yield CellMaker("setB", 2,
                    lambda args: SetDefaultInputCell('b', args.value))
    yield CellMaker("setR", 2,
                    lambda args: SetDefaultInputCell('r', args.value))
예제 #3
0
def _unsupported_gate(identifier: str, reason: str) -> CellMaker:
    def fail(_):
        raise NotImplementedError(
            f'Converting the Quirk gate {identifier} is not implemented yet. '
            f'Reason: {reason}')

    return CellMaker(identifier, 0, fail)
def _formula_gate(identifier: str, default_formula: str,
                  gate_func: Callable[[Union[sympy.Symbol, float]], 'cirq.Gate']
                 ) -> CellMaker:
    return CellMaker(identifier=identifier,
                     size=gate_func(0).num_qubits(),
                     maker=lambda args: gate_func(
                         parse_formula(default_formula if args.value is None
                                       else args.value)).on(*args.qubits))
예제 #5
0
def _input_rotation_gate(identifier: str, gate: 'cirq.Gate',
                         exponent_sign: int) -> CellMaker:
    return CellMaker(
        identifier, gate.num_qubits(),
        lambda args: InputRotationCell(identifier=identifier,
                                       register=None,
                                       base_operation=gate.on(args.qubits[0]),
                                       exponent_sign=exponent_sign))
예제 #6
0
def _reg_control(identifier: str, *, basis_change: Optional['cirq.SingleQubitGate']) -> CellMaker:
    return CellMaker(
        identifier=identifier,
        size=1,
        maker=lambda args: ControlCell(
            qubit=args.qubits[0], basis_change=_basis_else_empty(basis_change, args.qubits[0])
        ),
    )
예제 #7
0
def _permutation(identifier: str, name: str,
                 permutation: Tuple[int, ...]) -> CellMaker:
    return CellMaker(
        identifier,
        size=len(permutation),
        maker=lambda args: QuirkQubitPermutationGate(
            identifier=identifier, name=name, permutation=permutation).on(
                *args.qubits),
    )
예제 #8
0
def _input_family(identifier_prefix: str,
                  letter: str,
                  rev: bool = False) -> Iterator[CellMaker]:
    for n in CELL_SIZES:
        yield CellMaker(identifier=identifier_prefix + str(n),
                        size=n,
                        maker=lambda args: InputCell(qubits=args.qubits[::-1]
                                                     if rev else args.qubits,
                                                     letter=letter))
예제 #9
0
def _reg_parity_control(
    identifier: str, *, basis_change: Optional['cirq.SingleQubitGate'] = None
) -> CellMaker:
    return CellMaker(
        identifier=identifier,
        size=1,
        maker=lambda args: ParityControlCell(
            qubits=args.qubits, basis_change=_basis_else_empty(basis_change, args.qubits)
        ),
    )
예제 #10
0
def _measurement(identifier: str,
                 basis_change: Optional['cirq.Gate'] = None) -> CellMaker:
    return CellMaker(
        identifier=identifier,
        size=1,
        maker=lambda args: ExplicitOperationsCell(
            [ops.measure(*args.qubits, key=f'row={args.row},col={args.col}')],
            basis_change=cast(Iterable['cirq.Operation'],
                              [basis_change.on(*args.qubits)]
                              if basis_change else ())))
예제 #11
0
def _arithmetic_gate(identifier: str, size: int, func: _IntsToIntCallable) -> CellMaker:
    operation = _QuirkArithmeticCallable(func)
    assert identifier not in ARITHMETIC_OP_TABLE
    ARITHMETIC_OP_TABLE[identifier] = operation
    return CellMaker(
        identifier=identifier,
        size=size,
        maker=lambda args: ArithmeticCell(
            identifier=identifier, target=args.qubits, inputs=[None] * len(operation.letters)
        ),
    )
예제 #12
0
def _ignored_gate(identifier: str) -> CellMaker:
    # No matter the arguments (qubit, position, etc), map to nothing.
    return CellMaker(identifier, size=0, maker=lambda _: None)
def _gate(identifier: str, gate: 'cirq.Gate') -> CellMaker:
    return CellMaker(identifier=identifier,
                     size=gate.num_qubits(),
                     maker=lambda args: gate.on(*args.qubits))
예제 #14
0
def _scalar(identifier: str, operation: 'cirq.Operation') -> CellMaker:
    return CellMaker(identifier, size=1, maker=lambda _: operation)
예제 #15
0
def generate_all_swap_cell_makers() -> Iterator[CellMaker]:
    yield CellMaker("Swap", 1, lambda args: SwapCell(args.qubits, []))