quimb.tensor.circuit.qasm

Parsing of qsim and OpenQASM 2/3 into circuit gate lists.

Functions

_convert_ints_and_floats(x)

_put_registers_last(x)

parse_qsim_str(contents)

Parse a 'qsim' input format string into circuit information.

parse_qsim_file(fname, **kwargs)

Parse a qsim file.

parse_qsim_url(url, **kwargs)

Parse a qsim url.

to_clean_list(s, delimiter)

Split, strip and filter a string by a given character into a list.

multi_replace(s, replacements)

Replace multiple substrings in a string.

_openqasm_replace_tokens(s, replacements)

Replace whole identifier-like tokens in an OpenQASM fragment.

get_openqasm2_regexes()

get_openqasm3_regexes()

_openqasm_split_top_level(s[, sep])

_openqasm_eval_expr(expr, env)

_is_interface_placeholder(x)

_placeholder_param_vector(values)

parse_openqasm2_str(contents)

Parse the string contents of an OpenQASM 2.0 file. This parser does not

parse_openqasm2_file(fname, **kwargs)

Parse an OpenQASM 2.0 file.

parse_openqasm2_url(url, **kwargs)

Parse an OpenQASM 2.0 url.

parse_openqasm3_str(contents)

Parse an OpenQASM 3.0 program from a string.

parse_openqasm3_file(fname, **kwargs)

Parse an OpenQASM 3.0 file.

parse_openqasm3_url(url, **kwargs)

Parse an OpenQASM 3.0 program from a URL.

Module Contents

quimb.tensor.circuit.qasm._convert_ints_and_floats(x)[source]
quimb.tensor.circuit.qasm._put_registers_last(x)[source]
quimb.tensor.circuit.qasm.parse_qsim_str(contents)[source]

Parse a ‘qsim’ input format string into circuit information.

The format is described here: https://quantumai.google/qsim/input_format.

Parameters:

contents (str) – The full string of the qsim file.

Returns:

circuit_info – Information about the circuit:

  • circuit_info[‘n’]: the number of qubits

  • circuit_info[‘n_gates’]: the number of gates in total

  • circuit_info[‘gates’]: list[list[str]], list of gates, each of which is a list of strings read from a line of the qsim file.

Return type:

dict

quimb.tensor.circuit.qasm.parse_qsim_file(fname, **kwargs)[source]

Parse a qsim file.

quimb.tensor.circuit.qasm.parse_qsim_url(url, **kwargs)[source]

Parse a qsim url.

quimb.tensor.circuit.qasm.to_clean_list(s, delimiter)[source]

Split, strip and filter a string by a given character into a list.

quimb.tensor.circuit.qasm.multi_replace(s, replacements)[source]

Replace multiple substrings in a string.

quimb.tensor.circuit.qasm._openqasm_replace_tokens(s, replacements)[source]

Replace whole identifier-like tokens in an OpenQASM fragment.

quimb.tensor.circuit.qasm.get_openqasm2_regexes()[source]
quimb.tensor.circuit.qasm.get_openqasm3_regexes()[source]
quimb.tensor.circuit.qasm._openqasm_split_top_level(s, sep=',')[source]
quimb.tensor.circuit.qasm._openqasm_eval_expr(expr, env)[source]
quimb.tensor.circuit.qasm._is_interface_placeholder(x)[source]
quimb.tensor.circuit.qasm._placeholder_param_vector(values)[source]
quimb.tensor.circuit.qasm.parse_openqasm2_str(contents)[source]

Parse the string contents of an OpenQASM 2.0 file. This parser does not support classical control flow is not guaranteed to check the full openqasm grammar.

quimb.tensor.circuit.qasm.parse_openqasm2_file(fname, **kwargs)[source]

Parse an OpenQASM 2.0 file.

quimb.tensor.circuit.qasm.parse_openqasm2_url(url, **kwargs)[source]

Parse an OpenQASM 2.0 url.

quimb.tensor.circuit.qasm.parse_openqasm3_str(contents)[source]

Parse an OpenQASM 3.0 program from a string.

This parser is dependency free and supports a practical subset of OpenQASM 3 for circuit import, including qubit declarations, input declarations, arithmetic expressions, custom gates, and register broadcasting.

Parameters:

contents (str) – The OpenQASM 3 source code to parse.

Returns:

A dictionary describing the circuit with the following entries:

  • "n": total number of qubits.

  • "sitemap": mapping from OpenQASM qubit names to qubit indices.

  • "gates": parsed sequence of Gate objects.

  • "n_gates": total number of parsed gates.

  • "inputs": tuple of symbolic input names declared with input.

  • "symbols": mapping of symbolic names to their current values or symbolic placeholders.

  • "expressions": mapping from gate indices to symbolic parameter expressions requiring later binding.

Return type:

dict

Raises:
  • NotImplementedError – If the program uses unsupported OpenQASM 3 features such as control flow, calibration blocks, output declarations, or unsupported operations.

  • SyntaxError – If the source contains an instruction that does not match the supported grammar subset.

quimb.tensor.circuit.qasm.parse_openqasm3_file(fname, **kwargs)[source]

Parse an OpenQASM 3.0 file.

Parameters:
Returns:

The parsed circuit information returned by parse_openqasm3_str().

Return type:

dict

quimb.tensor.circuit.qasm.parse_openqasm3_url(url, **kwargs)[source]

Parse an OpenQASM 3.0 program from a URL.

Parameters:
Returns:

The parsed circuit information returned by parse_openqasm3_str().

Return type:

dict