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
- Raises
- to_string(circuit, version='0.1')[source]
Convert circuit to hiqasm.
- Parameters
- Returns
str, The HiQASM format of input circuit.
- Raises
TypeError – if circuit is not a Circuit.
TypeError – if version is not a str.
NotImplementedError – if HiQASM version not implement.
ValueError – if gate not implement in this version.
- 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
- Raises
- to_string(circuit, version='2.0')[source]
Convert circuit to hiqasm.
- Parameters
- Returns
str, The HiQASM format of input circuit.
- Raises
TypeError – if circuit is not a Circuit.
TypeError – if version is not a str.
NotImplementedError – if openqasm version not implement.
ValueError – if gate not implement in this version.
- mindquantum.io.bprint(strings: list, align=':', title='', v_around='=', h_around='|', fill_char=' ')[source]
Print the information in block shape.
- Parameters
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
- 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)──