Why did God create the tree of the knowledge of good and evil?

God demands love that is real. The only way to accomplish this is an option not to love. God did not create robots that are programmed to love God. Man had to choose to love God and the only way to choose to love God is a choice made available not to love God. The tree of the knowledge of good and evil provided that choice.

The Lord God took the man and put him in the Garden of Eden to work it and take care of it. And the Lord God commanded the man, “You are free to eat from any tree in the garden; but you must not eat from the tree of the knowledge of good and evil, for when you eat from it you will certainly die.”

Genesis 2:15-17