Exemplo n.º 1
0
def circuit_to_quirk_url(circuit: circuits.Circuit,
                         prefer_unknown_gate_to_failure: bool=False,
                         escape_url=True) -> str:
    """Returns a Quirk URL for the given circuit.

    Args:
        circuit: The circuit to open in Quirk.
        prefer_unknown_gate_to_failure: If not set, gates that fail to convert
            will cause this function to raise an error. If set, a URL
            containing bad gates will be generated. (Quirk will open the URL,
            and replace the bad gates with parse errors, but still get the rest
            of the circuit.)
        escape_url: If set, the generated URL will have special characters such
            as quotes escaped using %. This makes it possible to paste the URL
            into forums and the command line and etc and have it properly
            parse. If not set, the generated URL will be more compact and human
            readable (and can still be pasted directly into a browser's address
            bar).

    Returns:

    """
    circuit = circuit.copy()
    linearize_circuit_qubits(circuit)

    cols = []  # Type: List[List[Any]]
    for moment in circuit:
        can_merges = []
        for op in moment.operations:
            for col, can_merge in _to_quirk_cols(
                    op,
                    prefer_unknown_gate_to_failure):
                if can_merge:
                    can_merges.append(col)
                else:
                    cols.append(col)
        if can_merges:
            merged_col = [1] * max(len(e) for e in can_merges)
            for col in can_merges:
                for i in range(len(col)):
                    if col[i] != 1:
                        merged_col[i] = col[i]
            cols.append(merged_col)

    circuit_json = json.JSONEncoder(ensure_ascii=False,
                                    separators=(',', ':'),
                                    sort_keys=True).encode({'cols': cols})
    if escape_url:
        suffix = urllib.parse.quote(circuit_json)
    else:
        suffix = circuit_json
    return 'http://algassert.com/quirk#circuit={}'.format(suffix)
Exemplo n.º 2
0
def circuit_to_quirk_url(circuit: circuits.Circuit,
                         prefer_unknown_gate_to_failure: bool = False,
                         escape_url=True) -> str:
    """Returns a Quirk URL for the given circuit.

    Args:
        circuit: The circuit to open in Quirk.
        prefer_unknown_gate_to_failure: If not set, gates that fail to convert
            will cause this function to raise an error. If set, a URL
            containing bad gates will be generated. (Quirk will open the URL,
            and replace the bad gates with parse errors, but still get the rest
            of the circuit.)
        escape_url: If set, the generated URL will have special characters such
            as quotes escaped using %. This makes it possible to paste the URL
            into forums and the command line and etc and have it properly
            parse. If not set, the generated URL will be more compact and human
            readable (and can still be pasted directly into a browser's address
            bar).

    Returns:

    """
    circuit = circuit.copy()
    linearize_circuit_qubits(circuit)

    cols = []  # Type: List[List[Any]]
    for moment in circuit:
        can_merges = []
        for op in moment.operations:
            for col, can_merge in _to_quirk_cols(
                    op, prefer_unknown_gate_to_failure):
                if can_merge:
                    can_merges.append(col)
                else:
                    cols.append(col)
        if can_merges:
            merged_col = [1] * max(len(e) for e in can_merges)
            for col in can_merges:
                for i in range(len(col)):
                    if col[i] != 1:
                        merged_col[i] = col[i]
            cols.append(merged_col)

    circuit_json = json.JSONEncoder(ensure_ascii=False,
                                    separators=(',', ':'),
                                    sort_keys=True).encode({'cols': cols})
    if escape_url:
        suffix = urllib.parse.quote(circuit_json)
    else:
        suffix = circuit_json
    return f'http://algassert.com/quirk#circuit={suffix}'