mindquantum.io.OpenQASM

View Source On Gitee
class mindquantum.io.OpenQASM[source]

Convert a circuit to openqasm format.

Examples

>>> import numpy as np
>>> from mindquantum.core import Circuit
>>> from mindquantum.io import OpenQASM
>>> circuit = Circuit().rx(0.3, 0).z(0, 1).zz(np.pi, [0, 1])
>>> openqasm = OpenQASM()
>>> circuit_str = openqasm.to_string(circuit)
>>> print(circuit_str)
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
rx(0.3) q[0];
cz q[1],q[0];
rzz(6.283185307179586) q[0],q[1];
from_file(file_name)[source]

Read a openqasm file.

Parameters

file_name (str) – The path of file that stored quantum circuit in openqasm format.

Returns

Circuit, the quantum circuit translated from openqasm file.

from_string(string)[source]

Read a OpenQASM string.

Parameters

string (str) – The OpenQASM string of Circuit.

Returns

Circuit, the quantum circuit translated from OpenQASM string.

Examples

>>> from mindquantum.io import OpenQASM
>>> from mindquantum.core.circuit import Circuit
>>> circ = Circuit().x(0, 1).h(1)
>>> string = OpenQASM().to_string(circ)
>>> OpenQASM().from_string(string)
      ┏━━━┓
q0: ──┨╺╋╸┠─────────
      ┗━┳━┛
        ┃   ┏━━━┓
q1: ────■───┨ H ┠───
            ┗━━━┛
to_file(file_name, circuit, version='2.0')[source]

Convert a quantum circuit to openqasm format and save in file.

Parameters
  • file_name (str) – The file name you want to save the openqasm file.

  • circuit (Circuit) – The Circuit you want to convert.

  • version (str) – The version of openqasm. Default: '2.0'.

Raises
to_string(circuit, version='2.0')[source]

Convert circuit to openqasm.

Parameters
  • circuit (Circuit) – The quantum circuit you want to translated to openqasm.

  • version (str) – The openqasm version you want to use. Default: '2.0'.

Returns

str, The openqasm format of input circuit.

Raises