def circuit_from_qasm_string(qasm_string,
                             name=None,
                             basis_gates="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,"
                             "rx,ry,rz,cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,"
                             "cswap"):
    """Construct a quantum circuit from a qasm representation (string).

    Args:
        qasm_string (str): a string of qasm, or a filename containing qasm.
        basis_gates (str): basis gates for the quantum circuit.
        name (str or None): the name of the quantum circuit after loading
            qasm text into it. If no name given, assign automatically.
    Returns:
        QuantumCircuit: circuit constructed from qasm.
    Raises:
        QISKitError: if the string is not valid QASM
    """

    node_circuit = Qasm(data=qasm_string).parse()
    unrolled_circuit = Unroller(node_circuit,
                                CircuitBackend(basis_gates.split(",")))
    circuit_unrolled = unrolled_circuit.execute()
    if name:
        circuit_unrolled.name = name
    return circuit_unrolled
def load_unroll_qasm_file(filename, basis_gates='u1,u2,u3,cx,id'):
    """Load qasm file and return unrolled circuit

    Args:
        filename (str): a string for the filename including its location.
        basis_gates (str): basis to unroll circuit to.
    Returns:
        object: Returns a unrolled QuantumCircuit object
    """
    # create Program object Node (AST)
    node_circuit = qasm.Qasm(filename=filename).parse()
    node_unroller = Unroller(node_circuit, CircuitBackend(basis_gates.split(",")))
    circuit_unrolled = node_unroller.execute()
    return circuit_unrolled
Example #3
0
def load_qasm_string(qasm_string, name=None,
                     basis_gates="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,ry,rz,"
                                 "cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,cswap"):
    """Construct a quantum circuit from a qasm representation (string).

    Args:
        qasm_string (str): a string of qasm, or a filename containing qasm.
        basis_gates (str): basis gates for the quantum circuit.
        name (str or None): the name of the quantum circuit after loading qasm
            text into it. If no name given, assign automatically.
    Returns:
        QuantumCircuit: circuit constructed from qasm.
    Raises:
        QISKitError: if the string is not valid QASM
    """
    node_circuit = Qasm(data=qasm_string).parse()
    unrolled_circuit = Unroller(node_circuit, CircuitBackend(basis_gates.split(",")))
    circuit_unrolled = unrolled_circuit.execute()
    if name:
        circuit_unrolled.name = name
    return circuit_unrolled