Generate a program to compute the inverse quantum Fourier transform on a set of qubits.

    :param qubits: A list of qubit indexes.
    :return: A Quil program to compute the inverse Fourier transform of the qubits.
3998
def inverse_qft(qubits: List[int]) -> Program:
    
    
    qft_result = Program().inst(_core_qft(qubits, -1))
    qft_result += bit_reversal(qubits)
    inverse_qft = Program()
    while len(qft_result) > 0:
        new_inst = qft_result.pop()
        inverse_qft.inst(new_inst)
    return inverse_qft
1025050
def _construct_grover_circuit(self) -> None:
        
        oracle = Program()
        oracle_name = "GROVER_ORACLE"
        oracle.defgate(oracle_name, self.unitary_function_mapping)
        oracle.inst(tuple([oracle_name] + self.qubits))
        self.grover_circuit = self.oracle_grover(oracle, self.qubits)
85981
def set_q1q2(self, q1, q2, check=False):
        
        self.set_q(np.vstack([q1[np.newaxis,:,:], q2[np.newaxis,:,:]]))
        #self.q[0] = q1
        #self.q[1] = q2

        # initialize spectral PV
        #self.qh = self.fft2(self.q)

        # check that it works
        if check:
            np.testing.assert_allclose(self.q1, q1)
            np.testing.assert_allclose(self.q1, self.ifft2(self.qh1))
459608
def pyquil_to_tk(prog: Program) -> Circuit:
    
    reg_name = None
    qubits = prog.get_qubits()
    n_qubits = max(qubits) + 1
    tkc = Circuit(n_qubits)
    for i in prog.instructions:
        if isinstance(i, Gate):
            name = i.name
            try:
                optype = _known_quil_gate[name]
            except KeyError as error:
                raise NotImplementedError("Operation not supported by tket: " + str(i)) from error
            if len(i.params) == 0:
                tkc.add_operation(optype, [q.index for q in i.qubits])
            else:
                params = [p/PI for p in i.params]
                op = tkc._get_op(optype,len(i.qubits),len(i.qubits),params)
                tkc._add_operation(op, [q.index for q in i.qubits])
        elif isinstance(i, Measurement):
            if not i.classical_reg:
                raise NotImplementedError("Program has no defined classical register for measurement on qubit: ", i.qubits[0])
            reg = i.classical_reg
            if reg_name and reg_name != reg.name:
                raise NotImplementedError("Program has multiple classical registers: ", reg_name, reg.name)
            reg_name = reg.name
            op = tkc._get_op(OpType.Measure,1,1,str(reg.offset))
            tkc._add_operation(op, [i.qubit.index])
        elif isinstance(i, Declare):
            continue
        elif isinstance(i, Pragma):
            continue
        elif isinstance(i, Halt):
            return tkc
        else:
            raise NotImplementedError("Pyquil instruction is not a gate: " + str(i))
    return tkc
655298
def matrix(self) -> np.ndarray:
        
        num_qubits = self.num_qubits()
        if num_qubits is None:
            raise ValueError('Unknown number of qubits')
        num_dim = 2 ** num_qubits
        result = np.zeros((num_dim, num_dim), dtype=np.complex128)
        for gate, coefficient in self.items():
            result += protocols.unitary(gate) * coefficient
        return result
826759
def without_operations_touching(self, qubits: Iterable[raw_types.Qid]):
        
        qubits = frozenset(qubits)
        if not self.operates_on(qubits):
            return self
        return Moment(
            operation for operation in self.operations
            if qubits.isdisjoint(frozenset(operation.qubits)))
1828
def sZ(qubit: Qubit, coefficient: complex = 1.0) -> Pauli:
    """"""
    return Pauli.sigma(qubit, 'Z', coefficient)
780238
def make_grover_circuit(input_qubits, output_qubit, oracle):
    """"""
    # For 2 input qubits, that means using Grover operator only once.
    c = cirq.Circuit()

    # Initialize qubits.
    c.append([
        cirq.X(output_qubit),
        cirq.H(output_qubit),
        cirq.H.on_each(*input_qubits),
    ])

    # Query oracle.
    c.append(oracle)

    # Construct Grover operator.
    c.append(cirq.H.on_each(*input_qubits))
    c.append(cirq.X.on_each(*input_qubits))
    c.append(cirq.H.on(input_qubits[1]))
    c.append(cirq.CNOT(input_qubits[0], input_qubits[1]))
    c.append(cirq.H.on(input_qubits[1]))
    c.append(cirq.X.on_each(*input_qubits))
    c.append(cirq.H.on_each(*input_qubits))

    # Measure the result.
    c.append(cirq.measure(*input_qubits, key='result'))

    return c
455701
def permute(self, qubits: Qubits) -> 'Gate':
        """"""
        vec = self.vec.permute(qubits)
        return Gate(vec.tensor, qubits=vec.qubits)
795037
def program_unitary(program, n_qubits):
    
    umat = np.eye(2 ** n_qubits)
    for instruction in program:
        if isinstance(instruction, Gate):
            unitary = lifted_gate(gate=instruction, n_qubits=n_qubits)
            umat = unitary.dot(umat)
        else:
            raise ValueError("Can only compute program unitary for programs composed of `Gate`s")
    return umat
40023
def sY(qubit: Qubit, coefficient: complex = 1.0) -> Pauli:
    """"""
    return Pauli.sigma(qubit, 'Y', coefficient)
955503
def default_channel_ops(nqubits):
    
    for gates in cartesian_product(TOMOGRAPHY_GATES.values(), repeat=nqubits):
        yield qt.tensor(*gates)
764924
def _one_q_sic_prep(index, qubit):
    """"""
    if index == 0:
        return Program()

    theta = 2 * np.arccos(1 / np.sqrt(3))
    zx_plane_rotation = Program([
        RX(-pi / 2, qubit),
        RZ(theta - pi, qubit),
        RX(-pi / 2, qubit),
    ])

    if index == 1:
        return zx_plane_rotation

    elif index == 2:
        return zx_plane_rotation + RZ(-2 * pi / 3, qubit)

    elif index == 3:
        return zx_plane_rotation + RZ(2 * pi / 3, qubit)

    raise ValueError(f'Bad SIC index: {index}')
953421
def q(self, q):
        
        self._q = np.array(q)

        # mark other representations as outdated, will get generated on next
        # read
        self._euler = None
        self._dcm = None
503292
def qubits_count_tuple(qubits: Union[int, Qubits]) -> Tuple[int, Qubits]:
    """"""
    if isinstance(qubits, int):
        return qubits, tuple(range(qubits))
    return len(qubits), qubits
723929
def tk_to_pyquil(circ: Circuit) -> Program:
    
    p = Program()
    ro = p.declare('ro', 'BIT', circ.n_qubits())
    grid = circ._int_routing_grid()
    qubits = _grid_to_qubits(grid)
    slices = []
    for s in grid:
        news = set()
        for pair in s:
            if pair[0]>-1:
                news.add(pair[0])
        slices.append(news)
    for s in slices:
        for v in s:
            op = circ._unsigned_to_op(v)
            optype = op.get_type()
            if optype == OpType.Input or optype == OpType.Output:
                continue
            elif optype == OpType.Measure:
                p += Measurement(qubits[(v, 0)], ro[int(op.get_desc())])
                continue
            try:
                gatetype = _known_quil_gate_rev[optype]
            except KeyError as error:
                raise NotImplementedError("Cannot convert tket Op to pyquil gate: " + op.get_name()) from error
            params = [p*PI for p in op.get_params()]
            g = Gate(gatetype, params, [qubits[(v,port)] for port in range(op.get_n_inputs())])
            p += g
    return p
1141481
def operates_on(self, qubits: Iterable[raw_types.Qid]) -> bool:
        
        return any(q in qubits for q in self.qubits)