Coder Social home page Coder Social logo

fondq's People

Contributors

alexandrupaler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

recohen

fondq's Issues

"Simplify flipped CNOTs" exercise in intro colab has wrong solution

This isn't an issue with fondq, but I'm new to colabs, so I'm not sure how to suggest a fix to a colab. If there is a repo where I can submit a PR, then let me know.

In tutorial_01, "Introduction - Colab" from Cirq for NISQ near the end there is an exercise to create a Point Optimizer for CNOTs sandwiched with two H gates. The solution is only a redundant Hadamard optimizer. Perhaps the exercise changed after or vice versa. Anyhow, I wrote a HCXHOptimizer that you can throw in the Solution panel:

class HCXHOptimizer(cirq.PointOptimizer):
    """Replaces a CNOT sandwiched by H gates with a flipped CNOT."""
    
    # The circuit to improve
    # The index of the moment with the operation to focus on
    # The operation to focus improvements upon
    def optimization_at(self, circuit, index, op):
        if isinstance(op, cirq.GateOperation) and (op.gate == cirq.H):
          
            # Finds the index of the next moment that touches the given qubits.
            next_op_index = circuit.next_moment_operating_on(op.qubits, index + 1)
            qubit = op.qubits[0]
            
            # If the next index exists
            if next_op_index is not None:
              
                # Get the operation at the existing index
                next_op = circuit.operation_at(qubit, next_op_index)
                
                if isinstance(next_op, cirq.GateOperation) and  (next_op.gate == cirq.CNOT):
                    other_qubit = next_op.qubits[1]
                    cnot_index = next_op_index
                    cnot_op = next_op

                    next_op_index = circuit.next_moment_operating_on(op.qubits, next_op_index + 1)
                    next_op = circuit.operation_at(qubit, next_op_index)

                    if isinstance(next_op, cirq.GateOperation) and (next_op.gate == cirq.H):
                      other_op_index = circuit.prev_moment_operating_on(cnot_op.qubits, cnot_index)
                      other_op = circuit.operation_at(other_qubit, other_op_index)
                      
                      if isinstance(other_op, cirq.GateOperation) and (other_op.gate == cirq.H):
                        other_op_index = circuit.next_moment_operating_on(cnot_op.qubits, cnot_index + 1)
                        other_op = circuit.operation_at(other_qubit, other_op_index)

                        if isinstance(other_op, cirq.GateOperation) and (other_op.gate == cirq.H):                      
                          new_op = cirq.CNOT(other_qubit, qubit)
                        
                          # see https://cirq.readthedocs.io/en/stable/generated/cirq.PointOptimizationSummary.html?highlight=pointoptimizationsummary
                          return cirq.PointOptimizationSummary(
                              clear_span = next_op_index - index + 1, # Range of moments to affect. 
                              clear_qubits = cnot_op.qubits, # The set of qubits that should be cleared with each affected moment
                              new_operations = [new_op] # The operations to replace
                          )
        return None
                        
        
# The optimizer
opt = HCXHOptimizer()

a = cirq.NamedQubit("a")
b = cirq.NamedQubit("b")

# Unoptimized circuit
circuit = cirq.Circuit(cirq.H(a), cirq.H(b), cirq.CNOT(a, b), cirq.H(a), cirq.H(b))
print('Before\n{}\n'. format(circuit))

# Optimized circuit
opt.optimize_circuit(circuit)
print('After HCX opt\n{}\n'.format(circuit))

# We can remove the empty moments
remempty = cirq.DropEmptyMoments()
remempty.optimize_circuit(circuit)
print('After Drop opt\n{}\n'.format(circuit))

Can't install PyPI packages (cirq) for PyCall in Windows (Mac?)

This is related to how PyCall is installed in Windows / Mac as opposed to Linux.

From PyCall documentation :

By default on Mac and Windows systems, Pkg.add("PyCall") or Pkg.build("PyCall") will use the Conda.jl package to install a minimal Python distribution (via Miniconda) that is private to Julia (not in your PATH). You can use the Conda Julia package to install more Python packages, and import Conda to print the Conda.PYTHONDIR directory where python was installed. On GNU/Linux systems, PyCall will default to using the python3 program (if any, otherwise python) in your PATH. '

PyCall will by default use this julia only conda installation.

You can use Conda.jl to install packages using the methods described here. However the default installed version does not support installing PyPI packages with pip.

It is possible to get PyCall to use an existing Conda installation and point to a virtual env it will name conda_jl as described here. This env can be activated as usual with conda activate conda_jl from the terminal and packages can be installed directly. This workaround is less desirable than the following approach

The latest version experimental version of Conda.jl has support for PyPI packages and can be installed from julia by running,

Pkg.add(PackageSpec(name="Conda", rev="master")

You might need to run Pkg.build("Conda") if Conda is not build automatically. Now, you can run the following code to install a PyPI only package such as cirq,

using Conda
Conda.pip_interop(true)
Conda.pip("install", "cirq")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.