mindquantum.io

Input/Output module for MindQuantum.

class mindquantum.io.HiQASM[source]

Convert a circuit to hiqasm format.

Examples

>>> import numpy as np
>>> from mindquantum.io.qasm import HiQASM
>>> from mindquantum.core import Circuit
>>> circuit = Circuit().rx(0.3, 0).z(0, 1).zz(np.pi, [0, 1])
>>> hiqasm = HiQASM()
>>> circuit_str = hiqasm.to_string(circuit)
>>> print(circuit_str[68: 80])
CZ q[1],q[0]
>>> circuit_2 = hiqasm.from_string(circuit_str)
>>> circuit_2
q0: ──RX(3/10)────Z────ZZ(π)──
                  │      │
q1: ──────────────●────ZZ(π)──
from_file(file_name)[source]

Read a hiqasm file.

Parameters

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

Returns

Circuit, the quantum circuit translated from HiQASM file.

from_string(string)[source]

Read a hiqasm string

Parameters

string (str) – The HiQASM string of a Circuit.

Returns

Circuit, The quantum circuit translated from HiQASM string.

to_file(file_name, circuit, version='0.1')[source]

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

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

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

  • version (str) – The version of HiQASM. Default: ‘0.1’.

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

Convert circuit to hiqasm.

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

  • version (str) – The HiQASM version you want to use. Default: ‘0.1’.

Returns

str, The HiQASM format of input circuit.

Raises
class mindquantum.io.OpenQASM[source]

Convert a circuit to openqasm format

Examples

>>> import numpy as np
>>> from mindquantum.io.qasm import OpenQASM
>>> from mindquantum.core import Circuit
>>> circuit = Circuit().rx(0.3, 0).z(0, 1).zz(np.pi, [0, 1])
>>> openqasm = OpenQASM()
>>> circuit_str = openqasm.to_string(circuit)
>>> circuit_str[47:60]
'rx(0.3) q[0];'
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.

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 hiqasm.

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

  • version (str) – The HiQASM version you want to use. Default: ‘2.0’.

Returns

str, The HiQASM format of input circuit.

Raises
mindquantum.io.bprint(strings: list, align=':', title='', v_around='=', h_around='|', fill_char=' ')[source]

Print the information in block shape.

Parameters
  • strings (list[str]) – Information you want to output.

  • align (str) – The align char alone vertal direction. Default: “:”.

  • title (str) – The title of this information block. Default: “”.

  • v_around (str) – Vertical boundary char. Default: “=”.

  • h_around (str) – horizontal boundary char. Default: “|”.

  • fill_char (str) – Empty space fill with this char. Default: ” “.

Returns

list, a list of formatted string.

Examples

>>> from mindquantum.io import bprint
>>> title='Info of Bob'
>>> o = bprint(['Name:Bob', 'Age:17', 'Nationality:China'],
...     title=title)
>>> for i in o:
...     print(i)
====Info of Bob====
|Name       :Bob  |
|Age        :17   |
|Nationality:China|
===================
mindquantum.io.random_hiqasm(n_qubits, gate_num, version='0.1', seed=42)[source]

Generate random hiqasm supported circuit.

Parameters
  • n_qubits (int) – Total number of qubit in this quantum circuit.

  • gate_num (int) – Total number of gate in this quantum circuit.

  • version (str) – version of HIQASM. Default: ‘0.1’.

  • seed (int) – The random seed to generate this random quantum circuit. Default: 42.

Returns

str, quantum in HIQASM format.

Examples

>>> from mindquantum.io.qasm import random_hiqasm
>>> from mindquantum.io.qasm import HiQASM
>>> hiqasm_str = random_hiqasm(2, 5)
>>> hiqasm = HiQASM()
>>> circuit = hiqasm.from_string(hiqasm_str)
>>> circuit
q0: ──RZ(-2.513)────RZ(-3.012)────RX(0.738)────M(k0)───────────

q1: ──────S───────────────────────────●──────────Z──────M(k1)──