pyqpanda.Visualization.circuit_composer
Classes
CircuitComposer for representing quantum circuit using ascii art. |
Module Contents
- class pyqpanda.Visualization.circuit_composer.CircuitComposer(n_qubits: int)[源代码]
Bases:
pyqpanda.QCircuit
CircuitComposer for representing quantum circuit using ascii art.
Args
- n_qubits: int
Number of qubits of quantum circuit.
- circuit_layersList[List[str]]
List of each layer of quantum circuit, each layer of a list of string, representing each line of ascii art.
Examples
>>> n_qubits = 6 >>> qvm = pq.CPUQVM() >>> qvm.init_qvm() >>> q = qvm.qAlloc_many(n_qubits)
>>> circ1 = CircuitComposer(n_qubits) >>> circuit = pq.QCircuit() >>> circuit << pq.H(q[0]) << pq.CNOT(q[0], q[1]) << pq.CNOT(q[1], q[2])
>>> circ1.append(circuit) >>> circ1 << pq.BARRIER(q) >>> circ1.append(pq.QFT(q[4:]), "QFT") >>> print(circ1) ┌─┐ ! q_0: |0>─ ┤H├ ───■── ────── ────! ──────── └─┘ ┌──┴─┐ ! q_1: |0>─ ─── ┤CNOT├ ───■── ────! ──────── └────┘ ┌──┴─┐ ! q_2: |0>─ ─── ────── ┤CNOT├ ────! ──────── └────┘ ! q_3: |0>─ ─── ────── ────── ────! ──────── ! ┌──────┐ q_4: |0>─ ─── ────── ────── ────! ┤4 ├ ! │ QFT │ q_5: |0>─ ─── ────── ────── ────! ┤5 ├ ! └──────┘
>>> print(circ1.circuit) ┌─┐ ! q_0: |0>─┤H├ ───■── ────── ────! ─── ────────────── ─── ─ └─┘ ┌──┴─┐ ! q_1: |0>──── ┤CNOT├ ───■── ────! ─── ────────────── ─── ─ └────┘ ┌──┴─┐ ! q_2: |0>──── ────── ┤CNOT├ ────! ─── ────────────── ─── ─ └────┘ ! q_3: |0>──── ────── ────── ────! ─── ────────────── ─── ─ ! ┌─┐ q_4: |0>──── ────── ────── ────! ─── ───────■────── ┤H├ X ! ┌─┐ ┌──────┴─────┐ └─┘ │ q_5: |0>──── ────── ────── ────! ┤H├ ┤CR(1.570796)├ ─── X ! └─┘ └────────────┘
- append(circ: pyqpanda.QCircuit, name: str = '') None [源代码]
Append a quantum circuit.
Args
- circpq.QCircuit
Quantum circuit to be appended.
- namestr, optional
Name of the appended quantum circuit, by default None
Notes
If name is empty string, then append the circuit with its string representation not boxed.
The name must be in full English and cannot contain numbers or symbols
Even when a circuit is appended with a name, the composed circuit string can be get by using circuit property.
Examples
>>> circ = CircuitComposer(2) >>> circuit = pq.QCircuit() >>> circuit << pq.H(qubits[0]) << pq.CNOT(qubits[0], qubits[1]) >>> circ.append(circuit) >>> print(circ) ┌─┐ q_0: |0>─ ┤H├ ───■── └─┘ ┌──┴─┐ q_1: |0>─ ─── ┤CNOT├ └────┘
>>> circ.append(circuit, "GHZ") >>> print(circ) ┌─┐ ┌──────┐ q_0: |0>─ ┤H├ ───■── ┤0 ├ └─┘ ┌──┴─┐ │ GHZ │ q_1: |0>─ ─── ┤CNOT├ ┤1 ├ └────┘ └──────┘
>>> print(circ.circuit) ┌─┐ ┌─┐ q_0: |0>─┤H├ ───■── ┤H├ ───■── └─┘ ┌──┴─┐ └─┘ ┌──┴─┐ q_1: |0>──── ┤CNOT├ ─── ┤CNOT├ └────┘ └────┘
- insert(circ: pyqpanda.QCircuit)[源代码]
- draw_circuit(output=None, filename=None) str [源代码]
draw a quantum circuit.
Parameters
output : str,Output type: text、latex、pic
- filenamestr, optional
When the output type is pic, this parameter can be used to specify the path to save the picture
Returns
- str, if 'text' return ASII of circuit,
if 'latex' return latex of circuit, if 'pic' return picture save path of circuit,
Notes
If output is empty string, use text if filename is empty string,use 'QCircuit_pic.png'