def is_scalar_nonsparse_matrix(circuit, nqubits, identity_only):
 matrix = represent(Mul(*circuit), nqubits=nqubits)
 if isinstance(matrix, Number):
  return ((matrix == 1) if identity_only else True)
 else:
  matrix_trace = matrix.trace()
  adjusted_matrix_trace = ((matrix_trace / matrix[0]) if (not identity_only) else matrix_trace)
  is_identity = ((matrix[0] == 1.0) if identity_only else True)
  has_correct_trace = (adjusted_matrix_trace == pow(2, nqubits))
  return bool((matrix.is_diagonal() and has_correct_trace and is_identity))

def is_scalar_nonsparse_matrix ( circuit , nqubits , identity_only ) : matrix = represent ( Mul ( * circuit ) , nqubits = nqubits ) if isinstance ( matrix , Number ) : return ( ( matrix == 1 ) if identity_only else True ) else : matrix_trace = matrix . trace ( ) adjusted_matrix_trace = ( ( matrix_trace / matrix [ 0 ] ) if ( not identity_only ) else matrix_trace ) is_identity = ( ( matrix [ 0 ] == 1.0 ) if identity_only else True ) has_correct_trace = ( adjusted_matrix_trace == pow ( 2 , nqubits ) ) return bool ( ( matrix . is_diagonal ( ) and has_correct_trace and is_identity ) )
checks if a given circuit .