You don't need any code to see how far off your concept is. And the usage of the CHSH just covers up the issues by making the problems unnecessarily complicated. Some of your key hypotheses are:
i) That there is a predetermined polarization for entangled photons when they are created. (Of course, simply following the Bell argument should be enough to convince you this cannot be correct - that specific scenario is discussed.)
ii) That if a polarizer is set within 45 degrees of that value, the photon will pass through it 100% of the time (you do that to get the perfect correlations, see for example Nugatory's post #14 and your response in #15).
But hey, let's analyze this as it pertains to your specific approach.
a) Your entangled pairs produce 100% correlation at any identical angle. You satisfy this experimental requirement.
b) Photons produced by PDC pass through a polarizer if they within 45 degrees of the orientation value at creation. Oops! That is known to be false!
A single Type I PDC crystal produces pairs of entangled photons at KNOWN angles. And those photons do NOT all go through a polarizer set at any different angle, regardless of whether it is within 45 degrees or not. Instead, they pass through at the usual cos^2(theta) formula. That is completely in opposition to your b).
Note that these photons are entangled, but they are NOT polarization entangled. To get polarization entanglement, you need TWO such crystals oriented 90 degrees apart and placed very close together. By your logic, the output photons must come from one or the other of the two crystals. Either one individually ONLY produces pairs that fail your test. Keep in mind that these photons (from either crystal alone) are entangled, just not polarization entangled. So why would you need two crystals to cause them to become polarization entangled when they were already entangled on all other bases?
The answer is strictly quantum: The polarization entanglement comes from them being
indistinguishable as to which of the two is the source crystal.
This quantum element cannot properly be represented in any code. Your code will always produce the wrong results when you attempt to model a single Type I crystal output (making it compatible with experiment) and then attempting to model two Type I crystals as I described. Here is a link to a full description of Type I crystals (theory and experiment), which are used in about half of all entanglement experiments. (Note that Type II crystals, while seemingly avoiding this issue, actually exhibit an exactly identical problem. But explaining why is more complicated.)
Ultra-bright source of polarization-entangled photons (Kwiat et al, 1998)
If you cannot model the quantum element of
indistinguishability*, which determines whether polarization entanglement occurs, your entire premise fails
. Good luck modeling the real world!
*And in fact every single source of quantum entangled particles includes this critical requirement. It is actually possible to intentionally vary the amount of
indistinguishability from 100% down to 0%, and the resulting spin/polarization entanglement varies precisely along with it. If your concept were correct, once the photon pair is created (from the mother photon), it already has a specific angle for each of the daughter photons.