{"id":20126929,"url":"https://github.com/cduck/feynman_path","last_synced_at":"2025-05-06T17:35:48.591Z","repository":{"id":42179188,"uuid":"350937136","full_name":"cduck/feynman_path","owner":"cduck","description":"Visualization tool for the Feynman Path Integral applied to quantum circuits","archived":false,"fork":false,"pushed_at":"2023-02-26T10:53:51.000Z","size":544,"stargazers_count":51,"open_issues_count":3,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-09T08:07:19.828Z","etag":null,"topics":["feynman-integrals","path-integral","quantum-computing","visualization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cduck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-24T03:42:47.000Z","updated_at":"2024-11-08T09:33:01.000Z","dependencies_parsed_at":"2022-08-27T17:00:40.478Z","dependency_job_id":null,"html_url":"https://github.com/cduck/feynman_path","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cduck%2Ffeynman_path","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cduck%2Ffeynman_path/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cduck%2Ffeynman_path/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cduck%2Ffeynman_path/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cduck","download_url":"https://codeload.github.com/cduck/feynman_path/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224517548,"owners_count":17324455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["feynman-integrals","path-integral","quantum-computing","visualization"],"created_at":"2024-11-13T20:18:07.107Z","updated_at":"2024-11-13T20:18:07.853Z","avatar_url":"https://github.com/cduck.png","language":"Python","readme":"# Feynman Path Sum Diagram for Quantum Circuits\n\nA visualization tool for the Feynman Path Sum applied to quantum circuits.\nThe [path integral formulation](https://en.wikipedia.org/wiki/Path_integral_formulation) is an interpretation of quantum mechanics that can aid in understanding superposition and interference.\n\nPath sum:\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-entanglement.png\" width=\"991.5\" /\u003e\n\nCircuit diagram:\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-entanglement-circuit.png\" width=\"388\" /\u003e\n\nHow to read a path sum diagram:\n- Time flows from left to right as gates are executed on qubits.\n- Arrows transition from one state to another and traversing the arrows gives a path to an output.\n- Two diverging arrows indicate a split into two potential outcomes.\n- An orange arrow indicates a negative sign is added to that outcome.\n- When two arrows converge, the amplitudes are summed.\n- Quantum interference is when positive and negative amplitudes cancel in this sum.\n- The rightmost column lists the possible measurement outcomes along with the final probability amplitudes of measuring each outcome.\n\nSee also: [Bloch sphere visualization](https://github.com/cduck/bloch_sphere)\n\n# Install\n\nfeynman\\_path is available on PyPI:\n\n```bash\npython3 -m pip install feynman_path\n```\n\n## Prerequisites\n\nSeveral non-python tools are used to generate the graphics and various output formats.\nThese non-python dependencies are listed below and platform-specific installation instructions can be found [here](https://github.com/cduck/latextools/#prerequisites).\n\n- LaTeX: A distribution of LaTeX that provides the `pdflatex` command needs to be installed separately.  Used to generate the gate and state labels.\n- [pdf2svg](https://github.com/dawbarton/pdf2svg): Used to convert the LaTeX expressions into SVG elements.\n- [Inkscape](https://inkscape.org/) (optional): Only required to convert the output to PDF format.\n- [Cairo](https://www.cairographics.org/download/) (optional): Only required to convert the output to PNG format.\n\n### Ubuntu\n\n```bash\nsudo apt install texlive pdf2svg inkscape libcairo2  # Or texlive-latex-recommended, or texlive-latex-extra\n```\n\n### macOS\n\nUsing [homebrew](https://brew.sh/):\n\n```bash\nbrew install --cask mactex inkscape\nbrew install pdf2svg cairo\n```\n\n\n# Usage\n\nThis package provides a command line tool to generate diagrams.\n\n### Examples\n```bash\nfeynman_path interference 2 h0 cnot0,1 z1 h0 h1 cnot1,0 h1\n```\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/interference.png\" width=\"1355\" /\u003e\n\n```bash\nfeynman_path interference 2 h0 cnot0,1 z1 h0 h1 cnot1,0 h1 --circuit\n```\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/interference-circuit.png\" width=\"488\" /\u003e\n\n### Command line options\n```\n$ feynman_path -h\nusage: feynman_path [-h] [--svg] [--png] [--pdf] [--sequence] [--circuit]\n                    [--scale SCALE] [--verbose]\n                    name n_qubits gate [gate ...]\n\nRenders a Feynman path sum diagram for a sequence of quantum gates.\n\npositional arguments:\n  name           The file name to save (excluding file extension)\n  n_qubits       The number of qubits in the quantum circuit\n  gate           List of gates to apply (e.g. h0 z1 cnot0,1)\n\noptional arguments:\n  -h, --help     show this help message and exit\n  --svg          Save diagram as an SVG image (default)\n  --png          Save diagram as a PNG image\n  --pdf          Save diagram as a PDF document\n  --sequence     Save a sequence of images that build up the diagram from left\n                 to right as \u003cname\u003e-nn.svg/png/pdf\n  --circuit      Save a standard quantum circuit diagram named\n                 \u003cname\u003e-circuit.svg/png/pdf instead of a Feynman path diagram\n  --scale SCALE  Scales the resolution of the diagram when saved as a PNG\n  --verbose      Print extra progress information\n```\n\n### Python Package\nfeynman\\_path also provides a Python 3 package as an alternative to the command line tool.  Diagrams can be viewed directly in a Jupyter notebook or saved.\n\n```python\nimport feynman_path\n\nn_qubits = 3\nfont = 12\nws_label = 4+0.55*n_qubits  # Label width relative to font size\nw_time = 60+ws_label*font  # Diagram column width\nf = feynman_path.Diagram(\n    n_qubits, font=font, ws_label=ws_label, w_time=w_time)\n\nf.perform_h(0)\nf.perform_cnot(0, 1)\nf.perform_z(1)\nf.perform_cnot(1, 2, pre_latex=r'\\color{red!80!black}')\nf.perform_h(0)\nf.perform_h(1)\nf.perform_cnot(1, 0)\n\nf.draw()  # Display in Jupyter\n```\n```python\nf.draw().save_svg('output.svg')  # Save SVG\nf.draw().set_pixel_scale(2).save_png('output.png')  # Save PNG\nimport latextools\nlatextools.svg_to_pdf(f.draw()).save('output.pdf')  # Save PDF\n```\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-interference.png\" width=\"1626\" /\u003e\n\nSee [examples/render\\_examples.py](https://github.com/cduck/feynman_path/blob/master/examples/render_examples.py) for more example code.\n\n# Examples\n\n### Using the CNOT gate to entangle qubits\nThe [CNOT gate](https://en.wikipedia.org/wiki/Controlled_NOT_gate) (⋅–⨁) can be used to entangle two qubits, creating a [Bell pair](https://en.wikipedia.org/wiki/Bell_state), but for certain input qubit states, the CNOT will have no effect.\n\n**Create a Bell pair by using a CNOT on the |+0⟩ state (q0=|+⟩, q1=|0⟩):**\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/entanglement-circuit.png\" width=\"337.5\" /\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/entanglement.png\" width=\"810\" /\u003e\n\nNote the output (rightmost) column is an entangled state: |00⟩+|11⟩\n\n```bash\nfeynman_path no-entanglement 2 h0 cnot0,1 h0 h1\n```\n\n**Fail to create a bell pair by using a CNOT on the |++⟩ state (q0=|+⟩, q1=|+⟩):**\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-entanglement-circuit.png\" width=\"388\" /\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-entanglement.png\" width=\"991.5\" /\u003e\n\n```bash\nfeynman_path no-entanglement 2 h0 h1 cnot0,1 h0 h1\n```\n\nNote the output (rightmost) column is a separable state: |00⟩\n\n### Copying an intermediate qubit state onto an ancilla ruins interference\nIn classical computing, it is common to inspect intermediate steps of a computation.  This can be very useful for debugging.  In quantum computing however, this destroys the effect of interference.  We can use a [CNOT gate](https://en.wikipedia.org/wiki/Controlled_NOT_gate) (⋅–⨁) to copy an intermediate value onto another qubit to inspect later.  Shown below, copying the intermediate value of q1 to q2 changes the output of q0, q1.\n\n**Original circuit that compute the output q0=1, q1=0:**\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/interference-circuit.png\" width=\"488\" /\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/interference.png\" width=\"1355\" /\u003e\n\n```bash\nfeynman_path interference 2 h0 cnot0,1 z1 h0 h1 cnot1,0 h1\n```\n\n**The addition of CNOT1,2 to inspect the intermediate value of q1 changes the output of q0:**\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-interference-circuit.png\" width=\"538\" /\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/cduck/feynman_path/master/examples/no-interference.png\" width=\"1626\" /\u003e\n\nNote how the path diagram is the same except the arrows at H1 are now split into the upper and lower halves of the diagram and don't interfere anymore.\n\n```bash\nfeynman_path no-interference 3 h0 cnot0,1 z1 cnot1,2 h0 h1 cnot1,0 h1\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcduck%2Ffeynman_path","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcduck%2Ffeynman_path","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcduck%2Ffeynman_path/lists"}