Cannot print Circuit ascii diagram
Hi,
I created a basic circuit and tried to print it to console. It hit me with the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-5: ordinal not
in range(128)
My code is as follows:
import cirq
# Build a model using Circuits
circuit = cirq.Circuit()
# Create Qubits
(q0, q1) = cirq.LineQubit.range(2)
# Add Qubits to Circuit model using logic gates
circuit.append([cirq.H(q0), cirq.CNOT(q0, q1)])
# Measure the final values of the Qubits
circuit.append([cirq.measure(q0), cirq.measure(q1)])
# Visualising the Circuit
print (circuit)
Can someone tell me how I can fix this and visualise my circuit? Thanks!
This is likely a problem with your python environment's configuration, e.g. the default text encoding is not set to UTF8.
Are you on a windows machine? Are you using python 2 or python 3? If you try print(circuit.to_text_diagram(use_unicode_characters=False)) does that work as a workaround?
I'm working on macOS with a TensorFlow virtual environment with Python3.6. Let me try your suggestion and get back to you.
Cheers!
Yup, your code worked! Thank you so much!
How did you go about setting up your environment?
If people keep running into this snag, we might need to switch the default circuit representation to ASCII only.
It'd be wonderful if you can switch it to ASCII only! I tried it again and it failed. So might be a good move
Please tell us how you setup your environment. Other users might not have this problem.
I'm also having a problem with printing the circuit. I think it's a problem with windows command prompt not having good support for unicode. Strilanc's recommendation of print(circuit.to_text_diagram(use_unicode_characters=False)) worked for me, however when I tried running this code print(cirq.google.Bristlecone) I ran into the problem again. But there didn't seem to be a .to_text_diagram method. I tried various chcp codes to no affect. I also tried using a bash shell (on rish-16's program), but the output was nonsensical:
0: ΓöÇΓöÇΓöÇHΓöÇΓöÇΓöÇ@ΓöÇΓöÇΓöÇMΓöÇΓöÇΓöÇ
Γöé
1: ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇXΓöÇΓöÇΓöÇMΓöÇΓöÇΓöÇ
It would make things a lot easier if the default output was just ASCII.
Urgh, it is so frustrating for a modern OS to not understand monospaced UTF8.
I would really prefer not to make the default ASCII, because it's much harder to parse visually. But maybe it's necessary.
We could have a global switch to set it, like an equivalent of np.printoptions, but I dislike that kind of configuration because it's so easy for code in one file to break code in a file somewhere else.
We could have a global switch to set it, like an equivalent of
np.printoptions, but I dislike that kind of configuration because it's so easy for code in one file to break code in a file somewhere else.
That's why you set global switches in a if __name__ == "__main__": block to prevented from changing setting if that file is later imported. But ya, it would be so much easier if standards where... standard.
Edit: I've updated python from 3.5 to 3.7 and that fixed the problem for me. I heard updating to 3.6 also works but I haven't checked myself.