code: def unitary_operator(state_vector):
      
  
      if not np.allclose([np.linalg.norm(state_vector)], [1]):
          raise ValueError("Vector must be normalized")
  
      if 2 ** get_bits_needed(len(state_vector)) != len(state_vector):
          raise ValueError("Vector length must be a power of two and at least two")
  
      mat = np.identity(len(state_vector), dtype=complex)
      for i in range(len(state_vector)):
          mat[i, 0] = state_vector[i]
      U = np.linalg.qr(mat)[0]
  
      # make sure U|0> = |v>
      zero_state = np.zeros(len(U))
      zero_state[0] = 1
      if np.allclose(U.dot(zero_state), state_vector):
          return U
      else:
          # adjust phase if needed
          return -1 * U
docs: Uses QR factorization to create a unitary operator
      that can encode an arbitrary normalized vector into
      the wavefunction of a quantum state.
  
      Assumes that the state of the input qubits is to be expressed as
  
      .. math::
  
          (1, 0, \\ldots, 0)^T
  
      :param 1d array state_vector: Normalized vector whose length is at least
                                    two and a power of two.
      :return: Unitary operator that encodes state_vector
      :rtype: 2d array