def tensor_unitary(circuit: cirq.Circuit, qubits: Sequence[cirq.Qid] | None = None) -> np.ndarray:
    """Given a circuit contract a tensor network into a dense unitary
    of the circuit."""
    if qubits is None:
        qubits = sorted(circuit.all_qubits())

    tensors, qubit_frontier, _ = circuit_to_tensors(
        circuit=circuit, qubits=qubits, initial_state=None
    )
    tn = qtn.TensorNetwork(tensors)
    i_inds = tuple(f'i0_q{q}' for q in qubits)
    f_inds = tuple(f'i{qubit_frontier[q]}_q{q}' for q in qubits)
    tn.contract(inplace=True)
    return tn.to_dense(f_inds, i_inds)
Given a circuit contract a tensor network into a dense unitary
    of the circuit.
cirq-core/cirq/contrib/quimb/state_vector.py