The perfect sleight is a flawlessly coordinated act of deception. Magicians dedicate hours to bullying their fingers into precise positions and years mastering the art of misdirection. The best illusionists spend a lifetime honing their prestidigitations.

Now, there’s a computer can do it, too—all in a matter of seconds.

Yesterday programmers at Queen Mary University of London unveiled a system that uses artificial intelligence to perform magic tricks. The program not only calculates probabilities—it can capture every potential outcome of most card tricks—the system also evaluates how human spectators will perceive its performances, based on behavioral and cognitive studies. “When spectators view a magic trick, they are not experiencing a mathematical model,” says Howard Williams, a computer scientist at Queen Mary and co-creator of the system. “So we break down the trick into its mathematical and psychological components.”

The 12 magicians of Osiris

The first trick looks relatively simple. A wooden jigsaw puzzle depicts 12 magicians’ hands, each casting a spell. But rearrange the puzzle and two of the spells vanish. Hit “replay” a few times and it just gets frustrating—Where did those extra spells go?

The effect is based on a classic American puzzle, which sold over 10 million copies back in 1898. Called “Get Off the Earth,” it employs the same (frustrating) illusion. Keep an eye on this puzzle and you’ll notice that one of the 13 characters keeps disappearing as Earth moves.

Both tricks involve imperceptibly changing the lengths of some of the puzzle pieces but Williams wondered if science could improve on the illusion. “In magic jigsaw puzzles the way people see shapes and changes in length is the fundamental building block,” Williams says. “How much can you change the length of a shape before someone notices?”

In his study Williams conducted psychophysical experiments to figure out how much a magician can change the length of a rectangle before subjects notice that they have been fooled. He found that size increases of up to 20 percent could go unnoticed, especially when the subjects were distracted. He then programmed a computer with certain [HARD] rules (unbreakable tenets of a jigsaw construction, like: all pieces must eventually fill all gaps) and [OPTI] rules (ideal scenarios, like: minimize the number of rectangles that vanish entirely). The result is, more or less, a scientifically perfect magic trick. “I think this will generate more discussion and more experiments,” says Ronald Graham, mathematician and co-author of the book Magical Mathematics: The Mathematical Ideas That Animate Great Magic Tricks. “It’ll be interesting to see what serious magicians think about the idea of using machine learning as a way to optimize the shapes of the rectangles.”

A smart trick for your smartphone

This one is a bit more familiar. The magician deals out six cards and asks the spectator to reveal only the color of each card. The spectator then chooses one of them and the Phoney app miraculously predicts his or her card.

Williams put a lot of science into this illusion. Part of it was a matter of probability. “Think of it as a sequence of six, where each card can be a 1 or a 0, black or red,” Williams says. “That’s 26, or 64 different combinations. There are only 52 cards in a deck, so we have enough information.”

There was also a cognitive element. Williams studied card preferences, and found that subjects had a strong inclination toward high-ranking cards, red cards and hearts. He programmed his app to recognize these preferences when predicting which card the spectator had chosen.

During the actual performance, several illusions happen at once. First, a fresh deck of cards is cut but never actually shuffled. Then, as the spectator calls out each card’s color, the magician uses a fake lock screen—Watch the video again, carefully!—to enter that information into the system. Finally, the app combines preprogrammed preferences with probability calculations to choose the most likely card. “Magicians have a lot of experience, and a lot of that depends on the human component—[they] use the fact that human beings are what they are,” Graham says. “Of course, that’s all based on mathematical principles.”

Are robots ruining magic?
Williams was initially concerned that spectators might be less than impressed by a computer that could perform magic tricks. Part of the thrill, after all, is that a fellow human appears to have the miraculous ability to perform superhuman feats. “We were worried that people would be impressed, but that they wouldn’t really experience the trick—like watching a movie with CGI.” Williams says. Nevertheless, human beings seemed taken by the trick. In fact, Williams sent his optimized jigsaw puzzle to a London magic shop, where the entire stock sold out twice.

As for the science behind these optimized magic tricks, Graham says that he found the work compelling. “It was a serious attempt to try to understand the whole impact of magic—why it impresses people and why it’s amazing,” he says.

And perhaps magic is just the beginning. Graham suspects that in the future the artificial intelligence that powers these parlor tricks could influence the broader development of machine learning. “Artificial intelligence has always been promising to do great things,” he says. “This is just one avenue, where you try to use a computer’s ability to look at thousands and millions and billions of cases and optimize what is it that fools people.”